|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | + _ "github.com/orange-cloudavenue/cloudavenue-sdk-go" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + _ datasource.DataSource = &vdcsDataSource{} |
| 15 | + _ datasource.DataSourceWithConfigure = &vdcsDataSource{} |
| 16 | +) |
| 17 | + |
| 18 | +func NewVdcDataSource() datasource.DataSource { |
| 19 | + return &vdcDataSource{} |
| 20 | +} |
| 21 | + |
| 22 | +type vdcDataSource struct { |
| 23 | + client *CloudAvenueClient |
| 24 | +} |
| 25 | + |
| 26 | +type vdcDataSourceModel struct { |
| 27 | + ID types.String `tfsdk:"id"` |
| 28 | + VdcGroup types.String `tfsdk:"vdc_group"` |
| 29 | + Name types.String `tfsdk:"name"` |
| 30 | + Vdc *vdcDetail `tfsdk:"vdc"` |
| 31 | +} |
| 32 | + |
| 33 | +type vdcDetail struct { |
| 34 | + Name types.String `tfsdk:"name"` |
| 35 | + Description types.String `tfsdk:"description"` |
| 36 | + VdcServiceClass types.String `tfsdk:"vdc_service_class"` |
| 37 | + VdcDisponibilityClass types.String `tfsdk:"vdc_disponibility_class"` |
| 38 | + VdcBillingModel types.String `tfsdk:"vdc_billing_model"` |
| 39 | + VcpuInMhz2 types.Float64 `tfsdk:"vcpu_in_mhz2"` |
| 40 | + CPUAllocated types.Float64 `tfsdk:"cpu_allocated"` |
| 41 | + MemoryAllocated types.Float64 `tfsdk:"memory_allocated"` |
| 42 | + VdcStorageBillingModel types.String `tfsdk:"vdc_storage_billing_model"` |
| 43 | + VdcStorageProfiles []vdcStorageProfiles `tfsdk:"vdc_storage_profiles"` |
| 44 | +} |
| 45 | + |
| 46 | +type vdcStorageProfiles struct { |
| 47 | + Class types.String `tfsdk:"class"` |
| 48 | + Limit types.Int64 `tfsdk:"limit"` |
| 49 | + Default types.Bool `tfsdk:"default"` |
| 50 | +} |
| 51 | + |
| 52 | +func (d *vdcDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 53 | + resp.TypeName = req.ProviderTypeName + "_vdc" |
| 54 | +} |
| 55 | + |
| 56 | +func (d *vdcDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 57 | + resp.Schema = schema.Schema{ |
| 58 | + MarkdownDescription: "Show the vDC details.", |
| 59 | + |
| 60 | + Attributes: map[string]schema.Attribute{ |
| 61 | + "id": schema.StringAttribute{ |
| 62 | + Computed: true, |
| 63 | + }, |
| 64 | + "name": schema.StringAttribute{ |
| 65 | + MarkdownDescription: "VDC name.", |
| 66 | + Required: true, |
| 67 | + }, |
| 68 | + "vdc_group": schema.StringAttribute{ |
| 69 | + Computed: true, |
| 70 | + }, |
| 71 | + "vdc": schema.SingleNestedAttribute{ |
| 72 | + MarkdownDescription: "VDC details.", |
| 73 | + Computed: true, |
| 74 | + Attributes: map[string]schema.Attribute{ |
| 75 | + "name": schema.StringAttribute{ |
| 76 | + MarkdownDescription: "VDC name.", |
| 77 | + Computed: true, |
| 78 | + }, |
| 79 | + "description": schema.StringAttribute{ |
| 80 | + MarkdownDescription: "VDC UUID.", |
| 81 | + Computed: true, |
| 82 | + }, |
| 83 | + "vdc_service_class": schema.StringAttribute{ |
| 84 | + MarkdownDescription: "VDC service class.", |
| 85 | + Computed: true, |
| 86 | + }, |
| 87 | + "vdc_disponibility_class": schema.StringAttribute{ |
| 88 | + MarkdownDescription: "VDC disponibility class.", |
| 89 | + Computed: true, |
| 90 | + }, |
| 91 | + "vdc_billing_model": schema.StringAttribute{ |
| 92 | + MarkdownDescription: "VDC billing model.", |
| 93 | + Computed: true, |
| 94 | + }, |
| 95 | + "vcpu_in_mhz2": schema.NumberAttribute{ |
| 96 | + MarkdownDescription: "VDC CPU in Mhz2.", |
| 97 | + Computed: true, |
| 98 | + }, |
| 99 | + "cpu_allocated": schema.NumberAttribute{ |
| 100 | + MarkdownDescription: "VDC CPU allocated.", |
| 101 | + Computed: true, |
| 102 | + }, |
| 103 | + "memory_allocated": schema.NumberAttribute{ |
| 104 | + MarkdownDescription: "VDC memory allocated.", |
| 105 | + Computed: true, |
| 106 | + }, |
| 107 | + "vdc_storage_billing_model": schema.StringAttribute{ |
| 108 | + MarkdownDescription: "VDC storage billing model.", |
| 109 | + Computed: true, |
| 110 | + }, |
| 111 | + "vdc_storage_profiles": schema.ListNestedAttribute{ |
| 112 | + MarkdownDescription: "VDC storage profiles.", |
| 113 | + Computed: true, |
| 114 | + NestedObject: schema.NestedAttributeObject{ |
| 115 | + Attributes: map[string]schema.Attribute{ |
| 116 | + "class": schema.StringAttribute{ |
| 117 | + MarkdownDescription: "VDC storage profile class.", |
| 118 | + Computed: true, |
| 119 | + }, |
| 120 | + "limit": schema.NumberAttribute{ |
| 121 | + MarkdownDescription: "VDC storage profile limit.", |
| 122 | + Computed: true, |
| 123 | + }, |
| 124 | + "default": schema.BoolAttribute{ |
| 125 | + MarkdownDescription: "VDC storage profile default.", |
| 126 | + Computed: true, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func (d *vdcDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 138 | + // Prevent panic if the provider has not been configured. |
| 139 | + if req.ProviderData == nil { |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + client, ok := req.ProviderData.(*CloudAvenueClient) |
| 144 | + |
| 145 | + if !ok { |
| 146 | + resp.Diagnostics.AddError( |
| 147 | + "Unexpected Data Source Configure Type", |
| 148 | + fmt.Sprintf("Expected *CloudAvenueClient, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 149 | + ) |
| 150 | + |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + d.client = client |
| 155 | +} |
| 156 | + |
| 157 | +func (d *vdcDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 158 | + var data vdcDataSourceModel |
| 159 | + |
| 160 | + // Read Terraform configuration data into the model |
| 161 | + resp.Diagnostics.Append(req.Config.Get(ctx, &data)...) |
| 162 | + |
| 163 | + if resp.Diagnostics.HasError() { |
| 164 | + return |
| 165 | + } |
| 166 | + |
| 167 | + vdcs, _, err := d.client.VDCApi.ApiCustomersV20VdcsVdcNameGet(d.client.auth, data.Name.ValueString()) |
| 168 | + if err != nil { |
| 169 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read vdc detail, got error: %s", err)) |
| 170 | + return |
| 171 | + } |
| 172 | + |
| 173 | + var profiles []vdcStorageProfiles |
| 174 | + for _, profile := range vdcs.Vdc.VdcStorageProfiles { |
| 175 | + p := vdcStorageProfiles{ |
| 176 | + Class: types.StringValue(profile.Class), |
| 177 | + Limit: types.Int64Value(int64(profile.Limit)), |
| 178 | + Default: types.BoolValue(profile.Default_), |
| 179 | + } |
| 180 | + profiles = append(profiles, p) |
| 181 | + } |
| 182 | + |
| 183 | + data = vdcDataSourceModel{ |
| 184 | + VdcGroup: types.StringValue(vdcs.VdcGroup), |
| 185 | + Name: types.StringValue(vdcs.Vdc.Name), |
| 186 | + Vdc: &vdcDetail{ |
| 187 | + Name: types.StringValue(vdcs.Vdc.Name), |
| 188 | + Description: types.StringValue(vdcs.Vdc.Description), |
| 189 | + VdcServiceClass: types.StringValue(vdcs.Vdc.VdcServiceClass), |
| 190 | + VdcDisponibilityClass: types.StringValue(vdcs.Vdc.VdcDisponibilityClass), |
| 191 | + VdcBillingModel: types.StringValue(vdcs.Vdc.VdcBillingModel), |
| 192 | + VcpuInMhz2: types.Float64Value(vdcs.Vdc.VcpuInMhz2), |
| 193 | + CPUAllocated: types.Float64Value(vdcs.Vdc.CpuAllocated), |
| 194 | + MemoryAllocated: types.Float64Value(vdcs.Vdc.MemoryAllocated), |
| 195 | + VdcStorageBillingModel: types.StringValue(vdcs.Vdc.VdcStorageBillingModel), |
| 196 | + VdcStorageProfiles: profiles, |
| 197 | + }, |
| 198 | + } |
| 199 | + |
| 200 | + data.ID = types.StringValue("frangipane") |
| 201 | + |
| 202 | + // Save data into Terraform state |
| 203 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 204 | + if resp.Diagnostics.HasError() { |
| 205 | + return |
| 206 | + } |
| 207 | +} |
0 commit comments