/
resource_aviatrix_segmentation_security_domain_association.go
126 lines (105 loc) · 4.45 KB
/
resource_aviatrix_segmentation_security_domain_association.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
package aviatrix
import (
"fmt"
"log"
"strings"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v2/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixSegmentationSecurityDomainAssociation() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "Resource 'aviatrix_segmentation_security_domain_association' will be deprecated in future releases. Please use resource 'aviatrix_segmentation_network_domain_association' instead.",
Create: resourceAviatrixSegmentationSecurityDomainAssociationCreate,
Read: resourceAviatrixSegmentationSecurityDomainAssociationRead,
Delete: resourceAviatrixSegmentationSecurityDomainAssociationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"transit_gateway_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Transit Gateway name.",
},
"security_domain_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Security Domain name.",
},
"attachment_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Attachment name, either Spoke or Edge.",
},
},
}
}
func marshalSegmentationSecurityDomainAssociationInput(d *schema.ResourceData) *goaviatrix.SegmentationSecurityDomainAssociation {
return &goaviatrix.SegmentationSecurityDomainAssociation{
TransitGatewayName: d.Get("transit_gateway_name").(string),
SecurityDomainName: d.Get("security_domain_name").(string),
AttachmentName: d.Get("attachment_name").(string),
}
}
func resourceAviatrixSegmentationSecurityDomainAssociationCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
association := marshalSegmentationSecurityDomainAssociationInput(d)
d.SetId(association.TransitGatewayName + "~" + association.SecurityDomainName + "~" + association.AttachmentName)
flag := false
defer resourceAviatrixSegmentationSecurityDomainAssociationReadIfRequired(d, meta, &flag)
if err := client.CreateSegmentationSecurityDomainAssociation(association); err != nil {
return fmt.Errorf("could not create segmentation security domain association: %v", err)
}
return resourceAviatrixSegmentationSecurityDomainAssociationReadIfRequired(d, meta, &flag)
}
func resourceAviatrixSegmentationSecurityDomainAssociationReadIfRequired(d *schema.ResourceData, meta interface{}, flag *bool) error {
if !(*flag) {
*flag = true
return resourceAviatrixSegmentationSecurityDomainAssociationRead(d, meta)
}
return nil
}
func resourceAviatrixSegmentationSecurityDomainAssociationRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
transitGatewayName := d.Get("transit_gateway_name").(string)
securityDomainName := d.Get("security_domain_name").(string)
attachmentName := d.Get("attachment_name").(string)
if transitGatewayName == "" {
id := d.Id()
log.Printf("[DEBUG] Looks like an import, no segmentation_security_domain_association transit_gateway_name received. Import Id is %s", id)
d.SetId(id)
parts := strings.Split(id, "~")
transitGatewayName = parts[0]
securityDomainName = parts[1]
attachmentName = parts[2]
}
association := &goaviatrix.SegmentationSecurityDomainAssociation{
TransitGatewayName: transitGatewayName,
SecurityDomainName: securityDomainName,
AttachmentName: attachmentName,
}
_, err := client.GetSegmentationSecurityDomainAssociation(association)
if err == goaviatrix.ErrNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("could not find segmentation_security_domain_association %s: %v", transitGatewayName+"~"+securityDomainName+"~"+attachmentName, err)
}
d.Set("transit_gateway_name", transitGatewayName)
d.Set("security_domain_name", securityDomainName)
d.Set("attachment_name", attachmentName)
d.SetId(transitGatewayName + "~" + securityDomainName + "~" + attachmentName)
return nil
}
func resourceAviatrixSegmentationSecurityDomainAssociationDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
association := marshalSegmentationSecurityDomainAssociationInput(d)
if err := client.DeleteSegmentationSecurityDomainAssociation(association); err != nil {
return fmt.Errorf("could not delete segmentation_security_domain_association: %v", err)
}
return nil
}