Skip to content

Commit

Permalink
Add metadata API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lgarber-akamai committed Apr 18, 2023
1 parent 383e8da commit 7245316
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 4 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@ require (
sigs.k8s.io/yaml v1.2.0 // indirect
)

replace github.com/linode/linodego => /Users/lgarber/Projects/linodego/linodego

go 1.18
1 change: 1 addition & 0 deletions linode/image/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func readDataSource(ctx context.Context, d *schema.ResourceData, meta interface{

if image != nil {
d.SetId(image.ID)
d.Set("capabilities", image.Capabilities)
d.Set("label", image.Label)
d.Set("description", image.Description)
if image.Created != nil {
Expand Down
1 change: 1 addition & 0 deletions linode/image/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestAccDataSourceImage_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "type", "manual"),
resource.TestCheckResourceAttr(resourceName, "size", "1300"),
resource.TestCheckResourceAttr(resourceName, "vendor", "Debian"),
resource.TestCheckResourceAttrSet(resourceName, "capabilities"),
),
},
},
Expand Down
4 changes: 4 additions & 0 deletions linode/image/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func readResource(ctx context.Context, d *schema.ResourceData, meta interface{})
return diag.Errorf("Error getting Linode image %s: %s", d.Id(), err)
}

d.Set("capabilities", image.Capabilities)
d.Set("label", image.Label)
d.Set("description", image.Description)
d.Set("type", image.Type)
Expand Down Expand Up @@ -101,6 +102,7 @@ func createResourceFromLinode(
DiskID: diskID,
Label: d.Get("label").(string),
Description: d.Get("description").(string),
CloudInit: d.Get("cloud_init").(bool),
}

image, err := client.CreateImage(ctx, createOpts)
Expand Down Expand Up @@ -128,6 +130,7 @@ func createResourceFromUpload(
region := d.Get("region").(string)
label := d.Get("label").(string)
description := d.Get("description").(string)
cloudInit := d.Get("cloud_init").(bool)

imageReader, err := imageFromResourceData(d)
if err != nil {
Expand All @@ -144,6 +147,7 @@ func createResourceFromUpload(
Region: region,
Label: label,
Description: description,
CloudInit: cloudInit,
}

image, uploadURL, err := client.CreateImageUpload(ctx, createOpts)
Expand Down
2 changes: 2 additions & 0 deletions linode/image/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestAccImage_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resName, "size"),
resource.TestCheckResourceAttr(resName, "type", "manual"),
resource.TestCheckResourceAttr(resName, "is_public", "false"),
resource.TestCheckResourceAttr(resName, "capabilities.0", "cloud-init"),
resource.TestCheckResourceAttrSet(resName, "deprecated"),
),
},
Expand Down Expand Up @@ -123,6 +124,7 @@ func TestAccImage_update(t *testing.T) {
checkImageExists(resName, nil),
resource.TestCheckResourceAttr(resName, "label", imageName),
resource.TestCheckResourceAttr(resName, "description", "descriptive text"),
resource.TestCheckResourceAttrSet(resName, "capabilities"),
),
},
{
Expand Down
6 changes: 6 additions & 0 deletions linode/image/schema_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ var dataSourceSchema = map[string]*schema.Schema{
Description: "The unique ID assigned to this Image.",
Required: true,
},
"capabilities": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The capabilities of this Image.",
Computed: true,
},
"label": {
Type: schema.TypeString,
Description: "A short description of the Image. Labels cannot contain special characters.",
Expand Down
13 changes: 13 additions & 0 deletions linode/image/schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ var resourceSchema = map[string]*schema.Schema{
Description: "A detailed description of this Image.",
Optional: true,
},
"cloud_init": {
Type: schema.TypeBool,
Description: "Whether this image supports cloud-init.",
Optional: true,
Default: false,
ForceNew: true,
},
"created": {
Type: schema.TypeString,
Description: "When this Image was created.",
Expand Down Expand Up @@ -96,4 +103,10 @@ var resourceSchema = map[string]*schema.Schema{
Description: "The current status of this Image.",
Computed: true,
},
"capabilities": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "The capabilities of this Image.",
Computed: true,
},
}
1 change: 1 addition & 0 deletions linode/image/tmpl/basic.gotf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ resource "linode_image" "foobar" {
disk_id = "${linode_instance.foobar.disk.0.id}"
label = "{{.Image}}"
description = "descriptive text"
cloud_init = true
}

{{ end }}
1 change: 1 addition & 0 deletions linode/images/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func flattenImage(data interface{}) map[string]interface{} {
result := make(map[string]interface{})

result["id"] = image.ID
result["capabilities"] = image.Capabilities
result["label"] = image.Label
result["description"] = image.Description
result["created_by"] = image.CreatedBy
Expand Down
2 changes: 2 additions & 0 deletions linode/images/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func TestAccDataSourceImages_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "images.0.created_by"),
resource.TestCheckResourceAttrSet(resourceName, "images.0.size"),
resource.TestCheckResourceAttrSet(resourceName, "images.0.deprecated"),
resource.TestCheckResourceAttrSet(resourceName, "images.0.capabilities"),
resource.TestCheckResourceAttr(resourceName, "images.1.label", imageName),
resource.TestCheckResourceAttr(resourceName, "images.1.description", "descriptive text"),
resource.TestCheckResourceAttr(resourceName, "images.1.is_public", "false"),
Expand All @@ -51,6 +52,7 @@ func TestAccDataSourceImages_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "images.1.created_by"),
resource.TestCheckResourceAttrSet(resourceName, "images.1.size"),
resource.TestCheckResourceAttrSet(resourceName, "images.1.deprecated"),
resource.TestCheckResourceAttrSet(resourceName, "images.0.capabilities"),
),
},

Expand Down
9 changes: 5 additions & 4 deletions linode/images/schema_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var filterConfig = helper.FilterConfig{
"type": {APIFilterable: true, TypeFunc: helper.FilterTypeString},
"vendor": {APIFilterable: true, TypeFunc: helper.FilterTypeString},

"created_by": {TypeFunc: helper.FilterTypeString},
"id": {TypeFunc: helper.FilterTypeString},
"status": {TypeFunc: helper.FilterTypeString},
"description": {TypeFunc: helper.FilterTypeString},
"capabilities": {TypeFunc: helper.FilterTypeString},
"created_by": {TypeFunc: helper.FilterTypeString},
"id": {TypeFunc: helper.FilterTypeString},
"status": {TypeFunc: helper.FilterTypeString},
"description": {TypeFunc: helper.FilterTypeString},
}

var dataSourceSchema = map[string]*schema.Schema{
Expand Down
1 change: 1 addition & 0 deletions linode/instance/datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccDataSourceInstances_basic(t *testing.T) {
resource.TestCheckResourceAttr(resName, "instances.0.ipv4.#", "2"),
resource.TestCheckResourceAttrSet(resName, "instances.0.ipv6"),
resource.TestCheckResourceAttrSet(resName, "instances.0.host_uuid"),
resource.TestCheckResourceAttrSet(resName, "instances.0.has_user_data"),
resource.TestCheckResourceAttr(resName, "instances.0.disk.#", "2"),
resource.TestCheckResourceAttr(resName, "instances.0.config.#", "1"),
),
Expand Down
1 change: 1 addition & 0 deletions linode/instance/flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func flattenInstance(
result["tags"] = instance.Tags
result["image"] = instance.Image
result["host_uuid"] = instance.HostUUID
result["has_user_data"] = instance.HasUserData

result["backups"] = flattenInstanceBackups(*instance)
result["specs"] = flattenInstanceSpecs(*instance)
Expand Down
11 changes: 11 additions & 0 deletions linode/instance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func readResource(ctx context.Context, d *schema.ResourceData, meta interface{})
d.Set("tags", instance.Tags)
d.Set("booted", isInstanceBooted(instance))
d.Set("host_uuid", instance.HostUUID)
d.Set("has_user_data", instance.HasUserData)

flatSpecs := flattenInstanceSpecs(*instance)
flatAlerts := flattenInstanceAlerts(*instance)
Expand Down Expand Up @@ -180,6 +181,16 @@ func createResource(ctx context.Context, d *schema.ResourceData, meta interface{
}
}

if _, metadataOk := d.GetOk("metadata.0"); metadataOk {
var metadata linodego.InstanceMetadataOptions

if userData, userDataOk := d.GetOk("metadata.0.user_data"); userDataOk {
metadata.UserData = userData.(string)
}

createOpts.Metadata = &metadata
}

_, disksOk := d.GetOk("disk")
_, configsOk := d.GetOk("config")
bootedNull := d.GetRawConfig().GetAttr("booted").IsNull()
Expand Down
33 changes: 33 additions & 0 deletions linode/instance/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,39 @@ func TestAccResourceInstance_ipv4Sharing(t *testing.T) {
})
}

func TestAccResourceInstance_userData(t *testing.T) {
t.Parallel()

resName := "linode_instance.foobar"
var instance linodego.Instance
instanceName := acctest.RandomWithPrefix("tf_test")

resource.Test(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.TestAccProviders,
CheckDestroy: acceptance.CheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: tmpl.UserData(t, instanceName, "eu-west"),
Check: resource.ComposeTestCheckFunc(
acceptance.CheckInstanceExists(resName, &instance),
resource.TestCheckResourceAttr(resName, "label", instanceName),
resource.TestCheckResourceAttr(resName, "type", "g6-nanode-1"),
resource.TestCheckResourceAttr(resName, "image", acceptance.TestImageLatest),
resource.TestCheckResourceAttr(resName, "region", "eu-west"),
resource.TestCheckResourceAttr(resName, "has_user_data", "true"),
),
},
{
ResourceName: resName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"root_pass", "authorized_keys", "image", "resize_disk", "metadata"},
},
},
})
}

func TestAccResourceInstance_requestQuantity(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 5 additions & 0 deletions linode/instance/schema_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ var instanceDataSourceSchema = map[string]*schema.Schema{
Description: "The Linode’s host machine, as a UUID.",
Computed: true,
},
"has_user_data": {
Type: schema.TypeBool,
Description: "Whether this Instance was created with user-data.",
Computed: true,
},
"specs": {
Computed: true,
Type: schema.TypeList,
Expand Down
26 changes: 26 additions & 0 deletions linode/instance/schema_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ You may need to switch to an explicit disk configuration.
Take a look at the example here:
https://www.terraform.io/docs/providers/linode/r/instance.html#linode-instance-with-explicit-configs-and-disks`

func resourceMetadata() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"user_data": {
Type: schema.TypeString,
Optional: true,
Description: "The base64-encoded user-defined data exposed to this instance " +
"through the Linode Metadata service. Refer to the base64encode(...) function " +
"for information on encoding content for this field.",
ForceNew: true,
},
},
}
}

func resourceDeviceDisk() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -289,6 +304,17 @@ var resourceSchema = map[string]*schema.Schema{
Optional: true,
Computed: true,
},
"metadata": {
Type: schema.TypeList,
Elem: resourceMetadata(),
Description: "Various fields related to the Linode Metadata service.",
Optional: true,
},
"has_user_data": {
Type: schema.TypeBool,
Description: "Whether or not this Instance was created with user-data.",
Computed: true,
},
"specs": {
Computed: true,
Description: "Information about the resources available to this Linode.",
Expand Down
9 changes: 9 additions & 0 deletions linode/instance/tmpl/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,15 @@ func ManyLinodes(t *testing.T, label, pubKey, region string) string {
})
}

func UserData(t *testing.T, label, region string) string {
return acceptance.ExecuteTemplate(t,
"instance_userdata", TemplateData{
Label: label,
Image: acceptance.TestImageLatest,
Region: region,
})
}

func DataBasic(t *testing.T, label, region string) string {
return acceptance.ExecuteTemplate(t,
"instance_data_basic", TemplateData{
Expand Down
16 changes: 16 additions & 0 deletions linode/instance/tmpl/templates/userdata.gotf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{ define "instance_userdata" }}

resource "linode_instance" "foobar" {
label = "{{.Label}}"
type = "g6-nanode-1"
image = "{{.Image}}"
region = "{{ .Region }}"
root_pass = "myr00tp@ssw0rd!!!"
booted = false

metadata {
user_data = base64encode("myuserdata")
}
}

{{ end }}

0 comments on commit 7245316

Please sign in to comment.