|
| 1 | +package vcfa |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/vmware/go-vcloud-director/v3/govcd" |
| 10 | + "github.com/vmware/go-vcloud-director/v3/types/v56" |
| 11 | +) |
| 12 | + |
| 13 | +func datasourceVcfaOrgVdc() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + ReadContext: datasourceVcfaOrgVdcRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "org_id": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Required: true, |
| 21 | + Description: "Parent Organization ID", |
| 22 | + }, |
| 23 | + "region_id": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "Parent Region ID", |
| 27 | + }, |
| 28 | + "description": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Computed: true, |
| 31 | + Description: fmt.Sprintf("Description of the %s", labelVcfaOrgVdc), |
| 32 | + }, |
| 33 | + "name": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + Description: fmt.Sprintf("Name of the %s", labelVcfaOrgVdc), |
| 37 | + }, |
| 38 | + "supervisor_ids": { |
| 39 | + Type: schema.TypeSet, |
| 40 | + Computed: true, |
| 41 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 42 | + Description: fmt.Sprintf("A set of Supervisor IDs that back this %s", labelVcfaOrgVdc), |
| 43 | + }, |
| 44 | + "zone_resource_allocations": { |
| 45 | + Type: schema.TypeSet, |
| 46 | + Computed: true, |
| 47 | + Elem: orgVdcDsZoneResourceAllocation, |
| 48 | + Description: "A set of Region Zones and their resource allocations", |
| 49 | + }, |
| 50 | + "status": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: fmt.Sprintf("%s status", labelVcfaOrgVdc), |
| 54 | + }, |
| 55 | + }, |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +var orgVdcDsZoneResourceAllocation = &schema.Resource{ |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "region_zone_name": { |
| 62 | + Type: schema.TypeString, |
| 63 | + Computed: true, |
| 64 | + Description: fmt.Sprintf("%s Name", labelVcfaRegionZone), |
| 65 | + }, |
| 66 | + "region_zone_id": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: fmt.Sprintf("%s ID", labelVcfaRegionZone), |
| 70 | + }, |
| 71 | + "memory_limit_mib": { |
| 72 | + Type: schema.TypeInt, |
| 73 | + Computed: true, |
| 74 | + Description: "Memory limit in MiB", |
| 75 | + }, |
| 76 | + "memory_reservation_mib": { |
| 77 | + Type: schema.TypeInt, |
| 78 | + Computed: true, |
| 79 | + Description: "Memory reservation in MiB", |
| 80 | + }, |
| 81 | + "cpu_limit_mhz": { |
| 82 | + Type: schema.TypeInt, |
| 83 | + Computed: true, |
| 84 | + Description: "CPU limit in MHz", |
| 85 | + }, |
| 86 | + "cpu_reservation_mhz": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Computed: true, |
| 89 | + Description: "CPU reservation in MHz", |
| 90 | + }, |
| 91 | + }, |
| 92 | +} |
| 93 | + |
| 94 | +func datasourceVcfaOrgVdcRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 95 | + vcdClient := meta.(*VCDClient) |
| 96 | + getByNameAndOrgId := func(_ string) (*govcd.TmVdc, error) { |
| 97 | + region, err := vcdClient.GetRegionById(d.Get("region_id").(string)) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + org, err := vcdClient.GetOrgById(d.Get("org_id").(string)) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + return vcdClient.GetTmVdcByName(fmt.Sprintf("%s_%s", org.Org.Name, region.Region.Name)) |
| 106 | + } |
| 107 | + |
| 108 | + c := dsReadConfig[*govcd.TmVdc, types.TmVdc]{ |
| 109 | + entityLabel: labelVcfaOrgVdc, |
| 110 | + getEntityFunc: getByNameAndOrgId, |
| 111 | + stateStoreFunc: setTmVdcData, |
| 112 | + } |
| 113 | + return readDatasource(ctx, d, meta, c) |
| 114 | +} |
0 commit comments