-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add scaleway_user_data resource, update scaleway_server
- Loading branch information
Showing
9 changed files
with
458 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package scaleway | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccScalewayUserData_importBasic(t *testing.T) { | ||
resourceName := "scaleway_user_data.base" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckScalewayUserDataDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckScalewayUserDataConfig, | ||
}, | ||
|
||
resource.TestStep{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package scaleway | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
api "github.com/nicolai86/scaleway-sdk" | ||
) | ||
|
||
func resourceScalewayUserData() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceScalewayUserDataCreate, | ||
Read: resourceScalewayUserDataRead, | ||
Update: resourceScalewayUserDataUpdate, | ||
Delete: resourceScalewayUserDataDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"server": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "The server the meta data is associated with", | ||
}, | ||
"key": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "The key of the user data to manage", | ||
}, | ||
"value": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "The value of the user", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceScalewayUserDataCreate(d *schema.ResourceData, m interface{}) error { | ||
scaleway := m.(*Client).scaleway | ||
|
||
mu.Lock() | ||
if err := scaleway.PatchUserdata( | ||
d.Get("server").(string), | ||
d.Get("key").(string), | ||
[]byte(d.Get("value").(string)), | ||
false); err != nil { | ||
return err | ||
} | ||
mu.Unlock() | ||
|
||
d.SetId(fmt.Sprintf("userdata-%s-%s", d.Get("server").(string), d.Get("key").(string))) | ||
return resourceScalewayUserDataRead(d, m) | ||
} | ||
|
||
func resourceScalewayUserDataRead(d *schema.ResourceData, m interface{}) error { | ||
scaleway := m.(*Client).scaleway | ||
|
||
if d.Get("server").(string) == "" { | ||
// import case | ||
parts := strings.Split(d.Id(), "-") | ||
d.Set("key", parts[len(parts)-1]) | ||
d.Set("server", strings.Join(parts[1:len(parts)-1], "-")) | ||
} | ||
userdata, err := scaleway.GetUserdata( | ||
d.Get("server").(string), | ||
d.Get("key").(string), | ||
false, | ||
) | ||
|
||
if err != nil { | ||
if serr, ok := err.(api.APIError); ok { | ||
if serr.StatusCode == 404 { | ||
d.SetId("") | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
|
||
d.Set("value", userdata.String()) | ||
return nil | ||
} | ||
|
||
func resourceScalewayUserDataUpdate(d *schema.ResourceData, m interface{}) error { | ||
scaleway := m.(*Client).scaleway | ||
|
||
mu.Lock() | ||
if err := scaleway.PatchUserdata( | ||
d.Get("server").(string), | ||
d.Get("key").(string), | ||
[]byte(d.Get("value").(string)), | ||
false); err != nil { | ||
return err | ||
} | ||
mu.Unlock() | ||
|
||
return resourceScalewayUserDataRead(d, m) | ||
} | ||
|
||
func resourceScalewayUserDataDelete(d *schema.ResourceData, m interface{}) error { | ||
scaleway := m.(*Client).scaleway | ||
|
||
mu.Lock() | ||
defer mu.Unlock() | ||
|
||
err := scaleway.DeleteUserdata( | ||
d.Get("server").(string), | ||
d.Get("key").(string), | ||
false) | ||
if err != nil { | ||
return err | ||
} | ||
d.SetId("") | ||
return nil | ||
} |
Oops, something went wrong.