Skip to content

Commit

Permalink
added support for identifier in is_image datasource
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwal-ibm authored and hkantare committed Sep 2, 2021
1 parent 583131c commit 93f02c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
74 changes: 66 additions & 8 deletions ibm/data_source_ibm_is_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ func dataSourceIBMISImage() *schema.Resource {
Schema: map[string]*schema.Schema{

"name": {
Type: schema.TypeString,
Required: true,
Description: "Image name",
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"identifier", "name"},
Description: "Image name",
},

"identifier": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"identifier", "name"},
Description: "Image id",
},

"visibility": {
Expand Down Expand Up @@ -77,19 +85,27 @@ func dataSourceIBMISImage() *schema.Resource {
func dataSourceIBMISImageRead(d *schema.ResourceData, meta interface{}) error {

name := d.Get("name").(string)
identifier := d.Get("identifier").(string)
var visibility string
if v, ok := d.GetOk("visibility"); ok {
visibility = v.(string)
}

err := imageGet(d, meta, name, visibility)
if err != nil {
return err
if name != "" {
err := imageGetByName(d, meta, name, visibility)
if err != nil {
return err
}
} else if identifier != "" {
err := imageGetById(d, meta, identifier)
if err != nil {
return err
}
}

return nil
}

func imageGet(d *schema.ResourceData, meta interface{}, name, visibility string) error {
func imageGetByName(d *schema.ResourceData, meta interface{}, name, visibility string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
Expand Down Expand Up @@ -145,3 +161,45 @@ func imageGet(d *schema.ResourceData, meta interface{}, name, visibility string)

return fmt.Errorf("No image found with name %s", name)
}
func imageGetById(d *schema.ResourceData, meta interface{}, identifier string) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}

getImageOptions := &vpcv1.GetImageOptions{
ID: &identifier,
}

image, response, err := sess.GetImage(getImageOptions)
if err != nil {
if response.StatusCode == 404 {
return fmt.Errorf("No image found with id %s", identifier)
}
return fmt.Errorf("Error Fetching Images %s\n%s", err, response)
}

d.SetId(*image.ID)
d.Set("status", *image.Status)
if *image.Status == "deprecated" {
fmt.Printf("[WARN] Given image %s is deprecated and soon will be obsolete.", name)
}
d.Set("name", *image.Name)
d.Set("visibility", *image.Visibility)
d.Set("os", *image.OperatingSystem.Name)
d.Set("architecture", *image.OperatingSystem.Architecture)
d.Set("crn", *image.CRN)
if image.Encryption != nil {
d.Set("encryption", *image.Encryption)
}
if image.EncryptionKey != nil {
d.Set("encryption_key", *image.EncryptionKey.CRN)
}
if image.File != nil && image.File.Checksums != nil {
d.Set(isImageCheckSum, *image.File.Checksums.Sha256)
}
if image.SourceVolume != nil {
d.Set("source_volume", *image.SourceVolume.ID)
}
return nil
}
10 changes: 9 additions & 1 deletion website/docs/d/is_image.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ Retrieve information of an existing IBM Cloud Infrastructure image as a read-onl
data "ibm_is_image" "ds_image" {
name = "centos-7.x-amd64"
}
```
```terraform
data "ibm_is_image" "ds_image1" {
identifier = "d7bec597-4726-451f-8a63-e62e6f121c32c"
}
```

## Argument reference
Review the argument references that you can specify for your data source.

- `name` - (Required, String) The name of the image.
- `identifier` - (Optional, String) The id of the image.
**NOTE** : One of `identifier` or `name` is required
- `name` - (Optional, String) The name of the image.
**NOTE** : One of `identifier` or `name` is required
- `visibility` - (Optional, String) The visibility of the image. Accepted values are `public` or `private`.

## Attribute reference
Expand Down

0 comments on commit 93f02c1

Please sign in to comment.