Skip to content

Commit

Permalink
feat: data source instance image
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermoumoute committed Dec 4, 2019
1 parent bfc356b commit 24fa753
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
150 changes: 150 additions & 0 deletions scaleway/data_source_instance_image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package scaleway

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/instance/v1"
)

func dataSourceScalewayInstanceImage() *schema.Resource {
return &schema.Resource{
Read: dataSourceScalewayInstanceImageRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Description: "exact name of the desired image",
ConflictsWith: []string{"image_id"},
},
"image_id": {
Type: schema.TypeString,
Optional: true,
Description: "exact name of the desired image",
ConflictsWith: []string{"name", "architecture"},
},
"architecture": {
Type: schema.TypeString,
Optional: true,
Default: instance.ArchX86_64.String(),
Description: "architecture of the desired image",
ConflictsWith: []string{"image_id"},
},
"zone": zoneSchema(),
"organization_id": organizationIDSchema(),

"public": {
Type: schema.TypeBool,
Computed: true,
Description: "indication if the image is public",
},
"bootscript_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the bootscript associated with this image",
},
"root_volume_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the root volume associated with this image",
},
"additional_volume_ids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "The additional volume IDs attached to the image",
},
"from_server_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the server the image is originated from",
},
"creation_date": {
Type: schema.TypeString,
Computed: true,
Description: "date when the image was created",
},
"modification_date": {
Type: schema.TypeString,
Computed: true,
Description: "date when the image was updated",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "state of the image",
},
},
}
}

func dataSourceScalewayInstanceImageRead(d *schema.ResourceData, m interface{}) error {
meta := m.(*Meta)
instanceApi, zone, err := getInstanceAPIWithZone(d, meta)
if err != nil {
return err
}

imageID, ok := d.GetOk("image_id")
if !ok {
res, err := instanceApi.ListImages(&instance.ListImagesRequest{
Zone: zone,
Name: expandStringPtr(d.Get("name")),
Arch: expandStringPtr(d.Get("architecture")),
})
if err != nil {
return err
}
if len(res.Images) == 0 {
return fmt.Errorf("no image found with the name %s and architecture %s", d.Get("name"), d.Get("architecture"))
}
if len(res.Images) > 1 {
return fmt.Errorf("%d images found with the same name %s and architecture %s", len(res.Images), d.Get("name"), d.Get("architecture"))
}
imageID = res.Images[0].ID
}

resp, err := instanceApi.GetImage(&instance.GetImageRequest{
Zone: zone,
ImageID: imageID.(string),
})
if err != nil {
return err
}

d.SetId(newZonedId(zone, expandID(imageID)))
d.Set("security_group_id", d.Id())
d.Set("zone", zone)
d.Set("organization_id", resp.Image.Organization)
d.Set("architecture", resp.Image.Arch)
d.Set("name", resp.Image.Name)

d.Set("creation_date", flattenTime(&resp.Image.CreationDate))
d.Set("modification_date", flattenTime(&resp.Image.ModificationDate))
d.Set("public", resp.Image.Public)
d.Set("from_server_id", resp.Image.FromServer)
d.Set("state", resp.Image.State.String())

if resp.Image.DefaultBootscript != nil {
d.Set("bootscript_id", resp.Image.DefaultBootscript.ID)
} else {
d.Set("bootscript_id", "")
}

if resp.Image.RootVolume != nil {
d.Set("root_volume_id", resp.Image.RootVolume.ID)
} else {
d.Set("root_volume_id", "")
}

additionalVolumeIDs := []string(nil)
for _, volume := range orderVolumes(resp.Image.ExtraVolumes) {
additionalVolumeIDs = append(additionalVolumeIDs, volume.ID)
}
d.Set("additional_volume_ids", additionalVolumeIDs)

return nil
}
7 changes: 7 additions & 0 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@ func testAccGetResourceAttr(resourceName string, attrName string, dest *string)
}
}

func flattenTime(date *time.Time) interface{} {
if date != nil {
return date.Format(time.RFC3339)
}
return ""
}

func flattenDuration(duration *time.Duration) interface{} {
if duration != nil {
return duration.String()
Expand Down
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func Provider() terraform.ResourceProvider {
"scaleway_account_ssh_key": dataSourceScalewayAccountSSHKey(),
"scaleway_instance_security_group": dataSourceScalewayInstanceSecurityGroup(),
"scaleway_instance_server": dataSourceScalewayInstanceServer(),
"scaleway_instance_image": dataSourceScalewayInstanceImage(),
},
}

Expand Down

0 comments on commit 24fa753

Please sign in to comment.