-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_nsxt_vpc_subnet.go
151 lines (140 loc) · 4.16 KB
/
resource_nsxt_vpc_subnet.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
/***************************************************************************
* ========================================================================
* Copyright 2022-2023 VMware, Inc. All rights reserved. VMware Confidential
* SPDX-License-Identifier: MPL-2.0
* ========================================================================
*/
// Auto generated code. DO NOT EDIT.
// nolint
package nsxt
import (
nsxtclient "github.com/vmware/terraform-provider-nsxt-virtual-private-cloud/nsxt/clients"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceVpcSubnetSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"resource_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"advanced_config": {
Type: schema.TypeSet,
MaxItems: 1,
Optional: true,
Elem: resourceSubnetAdvancedConfigSchema(),
},
"dhcp_config": {
Type: schema.TypeSet,
MaxItems: 1,
Optional: true,
Elem: resourceVpcSubnetDhcpConfigSchema(),
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"_revision": {
Type: schema.TypeInt,
Computed: true,
},
"access_mode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"Private", "Public", "Isolated"}, false),
Default: "Private",
},
"ipv4_subnet_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"ip_addresses": {
Type: schema.TypeList,
Optional: true,
MaxItems: 2,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": {
Type: schema.TypeList,
Optional: true,
MaxItems: 30,
Elem: resourceTagSchema(),
},
"nsx_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"path": {
Type: schema.TypeString,
Computed: true,
},
}
}
func resourceNsxtVpcSubnet() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtVpcSubnetCreate,
Read: resourceNsxtVpcSubnetRead,
Update: resourceNsxtVpcSubnetUpdate,
Delete: resourceNsxtVpcSubnetDelete,
Schema: resourceVpcSubnetSchema(),
Importer: &schema.ResourceImporter{
State: resourceVpcSubnetImporter,
},
}
}
func resourceVpcSubnetImporter(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
s := resourceVpcSubnetSchema()
return ResourceImporter(d, m, "VpcSubnet", s, d.Id())
}
func resourceNsxtVpcSubnetRead(d *schema.ResourceData, meta interface{}) error {
s := resourceVpcSubnetSchema()
err := APIRead(d, meta, "VpcSubnet", s)
// if 404 not found error occurs, terraform should swallow it and not fail read on object
if err != nil && strings.Contains(err.Error(), "404") {
log.Printf("[WARNING] Failed to read object VpcSubnet %v\n", err)
return nil
}
return err
}
func resourceNsxtVpcSubnetCreate(d *schema.ResourceData, meta interface{}) error {
s := resourceVpcSubnetSchema()
err := APICreateOrUpdate(d, meta, "VpcSubnet", s)
if err == nil {
err = resourceNsxtVpcSubnetRead(d, meta)
}
return err
}
func resourceNsxtVpcSubnetUpdate(d *schema.ResourceData, meta interface{}) error {
s := resourceVpcSubnetSchema()
var err error
err = APICreateOrUpdate(d, meta, "VpcSubnet", s)
if err == nil {
err = resourceNsxtVpcSubnetRead(d, meta)
}
return err
}
func resourceNsxtVpcSubnetDelete(d *schema.ResourceData, meta interface{}) error {
nsxtClient := meta.(*nsxtclient.NsxtClient)
resourceID := d.Id()
if resourceID != "" {
path := nsxtClient.Config.BasePath + d.Get("path").(string)
err := nsxtClient.NsxtSession.Delete(path)
// if 'object not found' or 'forbidden' or 'success with no response' response occurs, terraform should swallow it and not fail apply on object, else throw error and fail
if err != nil && !(strings.Contains(err.Error(), "404") || strings.Contains(err.Error(), "204") || strings.Contains(err.Error(), "403")) {
log.Printf("[INFO] Error occurred in Delete for resource VpcSubnet \n")
return err
}
d.SetId("")
}
return nil
}