-
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.
- Loading branch information
1 parent
bfc356b
commit 24fa753
Showing
3 changed files
with
158 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,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 | ||
} |
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