Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to create images using 'data_source_reference' #156

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
33 changes: 30 additions & 3 deletions nutanix/resource_nutanix_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ func resourceNutanixImage() *schema.Resource {
Optional: true,
Computed: true,
},
"data_source_reference": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Computed: true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the field is marked as "Computed" then we should ideally read its value in resourceNutanixImageRead().
It calls out to https://www.nutanix.dev/api_references/prism-central-v3/#/b3A6MjU1ODc1MTE-get-a-existing-image API.
We can set this value in read function as well.

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"kind": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"uuid": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
},
},
"version": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -175,10 +195,11 @@ func resourceNutanixImageCreate(ctx context.Context, d *schema.ResourceData, met

_, iok := d.GetOk("source_uri")
_, pok := d.GetOk("source_path")
_, dsr := d.GetOk("data_source_reference")

// if both path and uri are provided, return an error
if iok && pok {
return diag.Errorf("both source_uri and source_path provided")
// if three path, uri, dsr are provided, return an error
if iok && pok && dsr {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if only two values are provided ?
Then also we should raise error.

return diag.Errorf("all three source_uri, source_path and data_source_reference are provided")
}

// Read Arguments and set request values
Expand Down Expand Up @@ -500,6 +521,12 @@ func getImageResource(d *schema.ResourceData, image *v3.ImageResources) error {
// set source uri
}

if dsr, dsrok := d.GetOk("data_source_reference"); dsrok {
dataSourceReference := dsr.(map[string]interface{})
image.DataSourceReference = validateRef(dataSourceReference)
image.ImageType = utils.StringPtr("DISK_IMAGE")
}

if csok {
checksum := cs.(map[string]interface{})
ca, caok := checksum["checksum_algorithm"]
Expand Down