|
| 1 | +package vra |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/vmware/vra-sdk-go/pkg/client/fabric_compute" |
| 8 | + "github.com/vmware/vra-sdk-go/pkg/models" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceFabricCompute() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + CreateContext: resourceFabricComputeCreate, |
| 17 | + ReadContext: resourceFabricComputeRead, |
| 18 | + UpdateContext: resourceFabricComputeUpdate, |
| 19 | + DeleteContext: resourceFabricComputeDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + StateContext: schema.ImportStatePassthroughContext, |
| 22 | + }, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "created_at": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Description: "Date when the entity was created. The date is in ISO 8601 and UTC.", |
| 29 | + }, |
| 30 | + "custom_properties": { |
| 31 | + Type: schema.TypeMap, |
| 32 | + Computed: true, |
| 33 | + Description: "A list of key value pair of custom properties for the fabric compute resource.", |
| 34 | + }, |
| 35 | + "description": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "A human-friendly description.", |
| 39 | + }, |
| 40 | + "external_id": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "The id of the external entity on the provider side.", |
| 44 | + }, |
| 45 | + "external_region_id": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Computed: true, |
| 48 | + Description: "The external region id of the fabric compute.", |
| 49 | + }, |
| 50 | + "external_zone_id": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "The external zone id of the fabric compute.", |
| 54 | + }, |
| 55 | + "lifecycle_state": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Computed: true, |
| 58 | + Description: "Lifecycle status of the compute instance.", |
| 59 | + }, |
| 60 | + "links": linksSchema(), |
| 61 | + "name": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: "A human-friendly name used as an identifier for the fabric compute resource instance.", |
| 65 | + }, |
| 66 | + "org_id": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "The id of the organization this entity belongs to.", |
| 70 | + }, |
| 71 | + "owner": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Email of the user that owns the entity.", |
| 75 | + }, |
| 76 | + "power_state": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Power state of fabric compute instance.", |
| 80 | + }, |
| 81 | + "tags": tagsSchema(), |
| 82 | + "type": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Computed: true, |
| 85 | + Description: "Type of the fabric compute instance.", |
| 86 | + }, |
| 87 | + "updated_at": { |
| 88 | + Type: schema.TypeString, |
| 89 | + Computed: true, |
| 90 | + Description: "Date when the entity was last updated. The date is ISO 8601 and UTC.", |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func resourceFabricComputeCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 97 | + return diag.FromErr(errors.New("vra_fabric_compute resources are only importable")) |
| 98 | +} |
| 99 | + |
| 100 | +func resourceFabricComputeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 101 | + apiClient := m.(*Client).apiClient |
| 102 | + |
| 103 | + id := d.Id() |
| 104 | + |
| 105 | + getResp, err := apiClient.FabricCompute.GetFabricCompute(fabric_compute.NewGetFabricComputeParams().WithID(id)) |
| 106 | + if err != nil { |
| 107 | + switch err.(type) { |
| 108 | + case *fabric_compute.GetFabricComputeNotFound: |
| 109 | + return diag.Errorf("fabric compute '%s' not found", id) |
| 110 | + default: |
| 111 | + // nop |
| 112 | + } |
| 113 | + return diag.FromErr(err) |
| 114 | + } |
| 115 | + |
| 116 | + fabricCompute := getResp.GetPayload() |
| 117 | + d.SetId(*fabricCompute.ID) |
| 118 | + d.Set("created_at", fabricCompute.CreatedAt) |
| 119 | + d.Set("custom_properties", fabricCompute.CustomProperties) |
| 120 | + d.Set("description", fabricCompute.Description) |
| 121 | + d.Set("external_id", fabricCompute.ExternalID) |
| 122 | + d.Set("external_region_id", fabricCompute.ExternalRegionID) |
| 123 | + d.Set("external_zone_id", fabricCompute.ExternalZoneID) |
| 124 | + d.Set("lifecycle_state", fabricCompute.LifecycleState) |
| 125 | + d.Set("name", fabricCompute.Name) |
| 126 | + d.Set("org_id", fabricCompute.OrgID) |
| 127 | + d.Set("owner", fabricCompute.Owner) |
| 128 | + d.Set("power_state", fabricCompute.PowerState) |
| 129 | + d.Set("type", fabricCompute.Type) |
| 130 | + d.Set("updated_at", fabricCompute.UpdatedAt) |
| 131 | + |
| 132 | + if err := d.Set("links", flattenLinks(fabricCompute.Links)); err != nil { |
| 133 | + return diag.Errorf("error setting zone links - error: %#v", err) |
| 134 | + } |
| 135 | + |
| 136 | + if err := d.Set("tags", flattenTags(fabricCompute.Tags)); err != nil { |
| 137 | + return diag.Errorf("error setting zone tags - error: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func resourceFabricComputeUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 144 | + apiClient := m.(*Client).apiClient |
| 145 | + |
| 146 | + id := d.Id() |
| 147 | + |
| 148 | + fabricComputeSpecification := &models.FabricComputeSpecification{ |
| 149 | + Tags: expandTags(d.Get("tags").(*schema.Set).List()), |
| 150 | + } |
| 151 | + |
| 152 | + if _, err := apiClient.FabricCompute.UpdateFabricCompute(fabric_compute.NewUpdateFabricComputeParams().WithID(id).WithBody(fabricComputeSpecification)); err != nil { |
| 153 | + return diag.FromErr(err) |
| 154 | + } |
| 155 | + |
| 156 | + return resourceFabricComputeRead(ctx, d, m) |
| 157 | +} |
| 158 | + |
| 159 | +func resourceFabricComputeDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 160 | + return nil |
| 161 | +} |
0 commit comments