Skip to content

Commit 49f38e0

Browse files
committed
feat: vdcs
1 parent 5dcae17 commit 49f38e0

File tree

7 files changed

+472
-0
lines changed

7 files changed

+472
-0
lines changed

docs/data-sources/vdc.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "cloudavenue_vdc Data Source - cloudavenue"
4+
subcategory: ""
5+
description: |-
6+
Show the vDC details.
7+
---
8+
9+
# cloudavenue_vdc (Data Source)
10+
11+
Show the vDC details.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) VDC name.
21+
22+
### Read-Only
23+
24+
- `id` (String) The ID of this resource.
25+
- `vdc` (Attributes) VDC details. (see [below for nested schema](#nestedatt--vdc))
26+
- `vdc_group` (String)
27+
28+
<a id="nestedatt--vdc"></a>
29+
### Nested Schema for `vdc`
30+
31+
Read-Only:
32+
33+
- `cpu_allocated` (Number) VDC CPU allocated.
34+
- `description` (String) VDC UUID.
35+
- `memory_allocated` (Number) VDC memory allocated.
36+
- `name` (String) VDC name.
37+
- `vcpu_in_mhz2` (Number) VDC CPU in Mhz2.
38+
- `vdc_billing_model` (String) VDC billing model.
39+
- `vdc_disponibility_class` (String) VDC disponibility class.
40+
- `vdc_service_class` (String) VDC service class.
41+
- `vdc_storage_billing_model` (String) VDC storage billing model.
42+
- `vdc_storage_profiles` (Attributes List) VDC storage profiles. (see [below for nested schema](#nestedatt--vdc--vdc_storage_profiles))
43+
44+
<a id="nestedatt--vdc--vdc_storage_profiles"></a>
45+
### Nested Schema for `vdc.vdc_storage_profiles`
46+
47+
Read-Only:
48+
49+
- `class` (String) VDC storage profile class.
50+
- `default` (Boolean) VDC storage profile default.
51+
- `limit` (Number) VDC storage profile limit.
52+
53+

docs/data-sources/vdcs.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "cloudavenue_vdcs Data Source - cloudavenue"
4+
subcategory: ""
5+
description: |-
6+
List all VDC inside an Organization.
7+
---
8+
9+
# cloudavenue_vdcs (Data Source)
10+
11+
List all VDC inside an Organization.
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Read-Only
19+
20+
- `id` (String) The ID of this resource.
21+
- `vdcs` (Attributes List) VDC list. (see [below for nested schema](#nestedatt--vdcs))
22+
23+
<a id="nestedatt--vdcs"></a>
24+
### Nested Schema for `vdcs`
25+
26+
Read-Only:
27+
28+
- `vdc_name` (String) VDC name.
29+
- `vdc_uuid` (String) VDC UUID.
30+
31+

internal/provider/provider.go

+2
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ func (p *cloudavenueProvider) DataSources(_ context.Context) []func() datasource
228228
NewPublicIPDataSource,
229229
NewEdgeGatewayDataSource,
230230
NewEdgeGatewaysDataSource,
231+
NewVdcsDataSource,
232+
NewVdcDataSource,
231233
}
232234
}
233235

internal/provider/vdc_datasource.go

+207
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
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+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package provider
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccVdcDataSource(t *testing.T) {
10+
dataSourceName := "data.cloudavenue_vdc.test"
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheck(t) },
13+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
14+
Steps: []resource.TestStep{
15+
// Read testing.
16+
{
17+
Config: testAccVdcDataSourceConfig,
18+
Check: resource.ComposeAggregateTestCheckFunc(
19+
resource.TestCheckResourceAttr(dataSourceName, "id", "frangipane"),
20+
),
21+
},
22+
},
23+
})
24+
}
25+
26+
const testAccVdcDataSourceConfig = `
27+
data "cloudavenue_vdc" "test" {
28+
name = "VDC_Frangipane"
29+
}
30+
`

0 commit comments

Comments
 (0)