/
resource_vcd_nsxt_alb_cloud.go
179 lines (151 loc) · 5.09 KB
/
resource_vcd_nsxt_alb_cloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package vcd
import (
"context"
"fmt"
"github.com/vmware/go-vcloud-director/v2/govcd"
"github.com/vmware/go-vcloud-director/v2/types/v56"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceVcdAlbCloud() *schema.Resource {
return &schema.Resource{
CreateContext: resourceVcdAlbCloudCreate,
ReadContext: resourceVcdAlbCloudRead,
// Update of NSX-T ALB Cloud configuration is not supported in VCD <= 10.4
// UpdateContext: resourceVcdAlbCloudUpdate,
DeleteContext: resourceVcdAlbCloudDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceVcdAlbCloudImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "NSX-T ALB Cloud name",
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "NSX-T ALB Cloud description",
},
"controller_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "NSX-T ALB Controller ID",
},
"importable_cloud_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "NSX-T ALB Importable Cloud ID",
},
"network_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Network pool ID for NSX-T ALB Importable Cloud",
},
"network_pool_name": {
Type: schema.TypeString,
Computed: true,
Description: "Network pool name of NSX-T ALB Cloud",
},
"health_status": {
Type: schema.TypeString,
Computed: true,
Description: "NSX-T ALB Cloud health status",
},
"health_message": {
Type: schema.TypeString,
Computed: true,
Description: "NSX-T ALB Cloud detailed health message",
},
},
}
}
func resourceVcdAlbCloudCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
if !vcdClient.Client.IsSysAdmin {
return diag.Errorf("this resource is only supported for Providers")
}
albCloudConfig := getNsxtAlbCloudType(d)
createdAlbCloud, err := vcdClient.CreateAlbCloud(albCloudConfig)
if err != nil {
return diag.Errorf("error creating NSX-T ALB Cloud '%s': %s", albCloudConfig.Name, err)
}
d.SetId(createdAlbCloud.NsxtAlbCloud.ID)
return resourceVcdAlbCloudRead(ctx, d, meta)
}
func resourceVcdAlbCloudRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
if !vcdClient.Client.IsSysAdmin {
return diag.Errorf("this resource is only supported for Providers")
}
albCloud, err := vcdClient.GetAlbCloudById(d.Id())
if err != nil {
if govcd.ContainsNotFound(err) {
d.SetId("")
return nil
}
return diag.Errorf("unable to find NSX-T ALB Cloud: %s", err)
}
setNsxtAlbCloudData(d, albCloud.NsxtAlbCloud)
return nil
}
func resourceVcdAlbCloudDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
if !vcdClient.Client.IsSysAdmin {
return diag.Errorf("this resource is only supported for Providers")
}
albCloud, err := vcdClient.GetAlbCloudById(d.Id())
if err != nil {
return diag.Errorf("unable to find NSX-T ALB Cloud: %s", err)
}
err = albCloud.Delete()
if err != nil {
return diag.Errorf("error deleting NSX-T ALB Cloud: %s", err)
}
return nil
}
func resourceVcdAlbCloudImport(_ context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
vcdClient := meta.(*VCDClient)
if !vcdClient.Client.IsSysAdmin {
return nil, fmt.Errorf("this resource is only supported for Providers")
}
resourceURI := d.Id()
albCloud, err := vcdClient.GetAlbCloudByName(resourceURI)
if err != nil {
return nil, fmt.Errorf("error finding NSX-T ALB Cloud with Name '%s': %s", d.Id(), err)
}
d.SetId(albCloud.NsxtAlbCloud.ID)
return []*schema.ResourceData{d}, nil
}
func getNsxtAlbCloudType(d *schema.ResourceData) *types.NsxtAlbCloud {
albCloudType := &types.NsxtAlbCloud{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
LoadBalancerCloudBacking: types.NsxtAlbCloudBacking{
BackingId: d.Get("importable_cloud_id").(string),
LoadBalancerControllerRef: types.OpenApiReference{
ID: d.Get("controller_id").(string),
},
},
NetworkPoolRef: &types.OpenApiReference{ID: d.Get("network_pool_id").(string)},
}
return albCloudType
}
func setNsxtAlbCloudData(d *schema.ResourceData, albCloud *types.NsxtAlbCloud) {
dSet(d, "name", albCloud.Name)
dSet(d, "description", albCloud.Description)
dSet(d, "health_status", albCloud.HealthStatus)
dSet(d, "health_message", albCloud.DetailedHealthMessage)
dSet(d, "importable_cloud_id", albCloud.LoadBalancerCloudBacking.BackingId)
dSet(d, "controller_id", albCloud.LoadBalancerCloudBacking.LoadBalancerControllerRef.ID)
if albCloud.NetworkPoolRef != nil {
dSet(d, "network_pool_name", albCloud.NetworkPoolRef.Name)
dSet(d, "network_pool_id", albCloud.NetworkPoolRef.ID)
}
}