@@ -2,6 +2,7 @@ package networkmanager
2
2
3
3
import (
4
4
"context"
5
+ "log"
5
6
"time"
6
7
7
8
"github.com/aws/aws-sdk-go/aws"
@@ -10,6 +11,7 @@ import (
10
11
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
12
13
"github.com/hashicorp/terraform-provider-aws/internal/conns"
14
+ "github.com/hashicorp/terraform-provider-aws/internal/tfresource"
13
15
)
14
16
15
17
// AttachmentAccepter does not require AttachmentType. However, querying attachments for status updates requires knowing tyupe
@@ -43,9 +45,8 @@ func ResourceAttachmentAccepter() *schema.Resource {
43
45
ForceNew : true ,
44
46
ValidateFunc : validation .StringInSlice ([]string {
45
47
networkmanager .AttachmentTypeVpc ,
48
+ networkmanager .AttachmentTypeSiteToSiteVpn ,
46
49
}, false ),
47
- // Implement Values() function for validation as more types are onboarded to provider
48
- // networkmanager.AttachmentType_Values(), false),
49
50
},
50
51
"core_network_arn" : {
51
52
Type : schema .TypeString ,
@@ -82,18 +83,38 @@ func ResourceAttachmentAccepter() *schema.Resource {
82
83
func resourceAttachmentAccepterCreate (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
83
84
conn := meta .(* conns.AWSClient ).NetworkManagerConn
84
85
85
- if attachmentType := d .Get ("attachment_type" ).(string ); attachmentType != networkmanager .AttachmentTypeVpc {
86
- return diag .Errorf ("unsupported Network Manager Attachment type: %s" , attachmentType )
87
- }
88
-
86
+ var state string
89
87
attachmentID := d .Get ("attachment_id" ).(string )
90
- vpcAttachment , err := FindVPCAttachmentByID (ctx , conn , attachmentID )
88
+ attachmentType := d .Get ("attachment_type" ).(string )
89
+
90
+ switch attachmentType {
91
+ case networkmanager .AttachmentTypeVpc :
92
+ vpcAttachment , err := FindVPCAttachmentByID (ctx , conn , attachmentID )
93
+
94
+ if err != nil {
95
+ return diag .Errorf ("reading Network Manager VPC Attachment (%s): %s" , attachmentID , err )
96
+ }
97
+
98
+ state = aws .StringValue (vpcAttachment .Attachment .State )
99
+
100
+ d .SetId (attachmentID )
101
+
102
+ case networkmanager .AttachmentTypeSiteToSiteVpn :
103
+ vpnAttachment , err := FindSiteToSiteVPNAttachmentByID (ctx , conn , attachmentID )
104
+
105
+ if err != nil {
106
+ return diag .Errorf ("reading Network Manager Site To Site VPN Attachment (%s): %s" , attachmentID , err )
107
+ }
108
+
109
+ state = aws .StringValue (vpnAttachment .Attachment .State )
110
+
111
+ d .SetId (attachmentID )
91
112
92
- if err != nil {
93
- return diag .Errorf ("reading Network Manager VPC Attachment (%s) : %s" , attachmentID , err )
113
+ default :
114
+ return diag .Errorf ("unsupported Network Manager Attachment type : %s" , attachmentType )
94
115
}
95
116
96
- if state := aws . StringValue ( vpcAttachment . Attachment . State ); state == networkmanager .AttachmentStatePendingAttachmentAcceptance || state == networkmanager .AttachmentStatePendingTagAcceptance {
117
+ if state == networkmanager .AttachmentStatePendingAttachmentAcceptance || state == networkmanager .AttachmentStatePendingTagAcceptance {
97
118
input := & networkmanager.AcceptAttachmentInput {
98
119
AttachmentId : aws .String (attachmentID ),
99
120
}
@@ -104,34 +125,72 @@ func resourceAttachmentAccepterCreate(ctx context.Context, d *schema.ResourceDat
104
125
return diag .Errorf ("accepting Network Manager Attachment (%s): %s" , attachmentID , err )
105
126
}
106
127
107
- if _ , err := waitVPCAttachmentCreated (ctx , conn , attachmentID , d .Timeout (schema .TimeoutCreate )); err != nil {
108
- return diag .Errorf ("waiting for Network Manager VPC Attachment (%s) create: %s" , attachmentID , err )
128
+ switch attachmentType {
129
+ case networkmanager .AttachmentTypeVpc :
130
+ if _ , err := waitVPCAttachmentCreated (ctx , conn , attachmentID , d .Timeout (schema .TimeoutCreate )); err != nil {
131
+ return diag .Errorf ("waiting for Network Manager VPC Attachment (%s) create: %s" , attachmentID , err )
132
+ }
133
+
134
+ case networkmanager .AttachmentTypeSiteToSiteVpn :
135
+ if _ , err := waitSiteToSiteVPNAttachmentAvailable (ctx , conn , attachmentID , d .Timeout (schema .TimeoutCreate )); err != nil {
136
+ return diag .Errorf ("waiting for Network Manager VPN Attachment (%s) create: %s" , attachmentID , err )
137
+ }
109
138
}
110
139
}
111
140
112
- d .SetId (attachmentID )
113
-
114
141
return resourceAttachmentAccepterRead (ctx , d , meta )
115
142
}
116
143
117
144
func resourceAttachmentAccepterRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
118
145
conn := meta .(* conns.AWSClient ).NetworkManagerConn
119
146
120
- vpcAttachment , err := FindVPCAttachmentByID (ctx , conn , d .Id ())
147
+ switch aType := d .Get ("attachment_type" ); aType {
148
+ case networkmanager .AttachmentTypeVpc :
149
+ vpcAttachment , err := FindVPCAttachmentByID (ctx , conn , d .Id ())
121
150
122
- if err != nil {
123
- return diag .Errorf ("reading Network Manager VPC Attachment (%s): %s" , d .Id (), err )
124
- }
151
+ if ! d .IsNewResource () && tfresource .NotFound (err ) {
152
+ log .Printf ("[WARN] Network Manager VPC Attachment %s not found, removing from state" , d .Id ())
153
+ d .SetId ("" )
154
+ return nil
155
+ }
156
+
157
+ if err != nil {
158
+ return diag .Errorf ("reading Network Manager VPC Attachment (%s): %s" , d .Id (), err )
159
+ }
160
+
161
+ a := vpcAttachment .Attachment
162
+ d .Set ("attachment_policy_rule_number" , a .AttachmentPolicyRuleNumber )
163
+ d .Set ("core_network_arn" , a .CoreNetworkArn )
164
+ d .Set ("core_network_id" , a .CoreNetworkId )
165
+ d .Set ("edge_location" , a .EdgeLocation )
166
+ d .Set ("owner_account_id" , a .OwnerAccountId )
167
+ d .Set ("resource_arn" , a .ResourceArn )
168
+ d .Set ("segment_name" , a .SegmentName )
169
+ d .Set ("state" , a .State )
170
+
171
+ case networkmanager .AttachmentTypeSiteToSiteVpn :
172
+ vpnAttachment , err := FindSiteToSiteVPNAttachmentByID (ctx , conn , d .Id ())
173
+
174
+ if ! d .IsNewResource () && tfresource .NotFound (err ) {
175
+ log .Printf ("[WARN] Network Manager Site To Site VPN Attachment %s not found, removing from state" , d .Id ())
176
+ d .SetId ("" )
177
+ return nil
178
+ }
125
179
126
- a := vpcAttachment .Attachment
127
- d .Set ("attachment_policy_rule_number" , a .AttachmentPolicyRuleNumber )
128
- d .Set ("core_network_arn" , a .CoreNetworkArn )
129
- d .Set ("core_network_id" , a .CoreNetworkId )
130
- d .Set ("edge_location" , a .EdgeLocation )
131
- d .Set ("owner_account_id" , a .OwnerAccountId )
132
- d .Set ("resource_arn" , a .ResourceArn )
133
- d .Set ("segment_name" , a .SegmentName )
134
- d .Set ("state" , a .State )
180
+ if err != nil {
181
+ return diag .Errorf ("reading Network Manager Site To Site VPN Attachment (%s): %s" , d .Id (), err )
182
+ }
183
+
184
+ a := vpnAttachment .Attachment
185
+ d .Set ("attachment_policy_rule_number" , a .AttachmentPolicyRuleNumber )
186
+ d .Set ("core_network_arn" , a .CoreNetworkArn )
187
+ d .Set ("core_network_id" , a .CoreNetworkId )
188
+ d .Set ("edge_location" , a .EdgeLocation )
189
+ d .Set ("owner_account_id" , a .OwnerAccountId )
190
+ d .Set ("resource_arn" , a .ResourceArn )
191
+ d .Set ("segment_name" , a .SegmentName )
192
+ d .Set ("state" , a .State )
193
+ }
135
194
136
195
return nil
137
196
}
0 commit comments