/
resource_aviatrix_spoke_gateway_subnet_group.go
164 lines (134 loc) · 5.02 KB
/
resource_aviatrix_spoke_gateway_subnet_group.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
package aviatrix
import (
"context"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v3/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixSpokeGatewaySubnetGroup() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceAviatrixSpokeGatewaySubnetGroupCreate,
ReadWithoutTimeout: resourceAviatrixSpokeGatewaySubnetGroupRead,
UpdateWithoutTimeout: resourceAviatrixSpokeGatewaySubnetGroupUpdate,
DeleteWithoutTimeout: resourceAviatrixSpokeGatewaySubnetGroupDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Subnet group name.",
},
"gw_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Spoke gateway name.",
},
"subnets": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "A set of subnets in the subnet group.",
},
},
}
}
func marshalSpokeGatewaySubnetGroupInput(d *schema.ResourceData) *goaviatrix.SpokeGatewaySubnetGroup {
var subnets []string
for _, subnet := range d.Get("subnets").(*schema.Set).List() {
subnets = append(subnets, subnet.(string))
}
spokeGatewaySubnetGroup := &goaviatrix.SpokeGatewaySubnetGroup{
SubnetGroupName: d.Get("name").(string),
GatewayName: d.Get("gw_name").(string),
SubnetList: subnets,
}
return spokeGatewaySubnetGroup
}
func resourceAviatrixSpokeGatewaySubnetGroupCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
spokeGatewaySubnetGroup := marshalSpokeGatewaySubnetGroupInput(d)
d.SetId(spokeGatewaySubnetGroup.GatewayName + "~" + spokeGatewaySubnetGroup.SubnetGroupName)
flag := false
defer resourceAviatrixSpokeGatewaySubnetGroupReadIfRequired(ctx, d, meta, &flag)
if len(spokeGatewaySubnetGroup.SubnetList) == 0 {
if err := client.AddSpokeGatewaySubnetGroup(ctx, spokeGatewaySubnetGroup); err != nil {
return diag.Errorf("could not create an empty spoke gateway subnet group: %v", err)
}
} else {
if err := client.UpdateSpokeGatewaySubnetGroup(ctx, spokeGatewaySubnetGroup); err != nil {
return diag.Errorf("could not create spoke gateway subnet group: %v", err)
}
}
return resourceAviatrixSpokeGatewaySubnetGroupReadIfRequired(ctx, d, meta, &flag)
}
func resourceAviatrixSpokeGatewaySubnetGroupReadIfRequired(ctx context.Context, d *schema.ResourceData, meta interface{}, flag *bool) diag.Diagnostics {
if !(*flag) {
*flag = true
return resourceAviatrixSpokeGatewaySubnetGroupRead(ctx, d, meta)
}
return nil
}
func resourceAviatrixSpokeGatewaySubnetGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
name := d.Get("name").(string)
if name == "" {
id := d.Id()
log.Printf("[DEBUG] Looks like an import. Import Id is %s", id)
parts := strings.Split(id, "~")
if len(parts) != 2 {
return diag.Errorf("invalid ID, expected ID gw_name~name, instead got %s", d.Id())
}
d.Set("gw_name", parts[0])
d.Set("name", parts[1])
d.SetId(id)
}
name = d.Get("name").(string)
spokeGatewayName := d.Get("gw_name").(string)
spokeGatewaySubnetGroup := &goaviatrix.SpokeGatewaySubnetGroup{
GatewayName: spokeGatewayName,
SubnetGroupName: name,
}
err := client.GetSpokeGatewaySubnetGroup(ctx, spokeGatewaySubnetGroup)
if err != nil {
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
return diag.Errorf("couldn't get the spoke gateway subnet group %s due to %v", name, err)
}
if err := d.Set("subnets", spokeGatewaySubnetGroup.SubnetList); err != nil {
return diag.Errorf("could not set subnets: %v", err)
}
d.SetId(spokeGatewayName + "~" + name)
return nil
}
func resourceAviatrixSpokeGatewaySubnetGroupUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
flag := false
defer resourceAviatrixSpokeGatewaySubnetGroupReadIfRequired(ctx, d, meta, &flag)
client := meta.(*goaviatrix.Client)
if d.HasChange("subnets") {
spokeGatewaySubnetGroup := marshalSpokeGatewaySubnetGroupInput(d)
err := client.UpdateSpokeGatewaySubnetGroup(ctx, spokeGatewaySubnetGroup)
if err != nil {
return diag.Errorf("could not update spoke gateway subnet group: %v", err)
}
}
return resourceAviatrixSpokeGatewaySubnetGroupReadIfRequired(ctx, d, meta, &flag)
}
func resourceAviatrixSpokeGatewaySubnetGroupDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*goaviatrix.Client)
spokeGatewaySubnetGroup := marshalSpokeGatewaySubnetGroupInput(d)
err := client.DeleteSpokeGatewaySubnetGroup(ctx, spokeGatewaySubnetGroup)
if err != nil {
return diag.Errorf("could not delete spoke gateway subnet group: %v", err)
}
return nil
}