Skip to content

Commit 9d8088a

Browse files
authored
Merge pull request akshaykarle#95 from koalificationio/fix-peering
Fix peering
2 parents 631a8cd + 0506095 commit 9d8088a

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

mongodbatlas/resource_mongodbatlas_vpc_peering_connection.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func resourceVpcPeeringConnection() *schema.Resource {
2222
State: resourceVpcPeeringConnectionImportState,
2323
},
2424

25+
SchemaVersion: 1,
26+
StateUpgraders: []schema.StateUpgrader{
27+
{
28+
Type: resourceVpcPeeringConnectionResourceV0().CoreConfigSchema().ImpliedType(),
29+
Upgrade: resourceVpcPeeringConnectionStateUpgradeV0,
30+
Version: 0,
31+
},
32+
},
33+
2534
Schema: map[string]*schema.Schema{
2635
"group": {
2736
Type: schema.TypeString,
@@ -300,6 +309,18 @@ func resourceVpcPeeringConnectionImportState(d *schema.ResourceData, meta interf
300309
return nil, err
301310
}
302311

312+
// https://docs.atlas.mongodb.com/reference/api/vpc-get-connection/#example-response
313+
// Atlas API does not return ProviderName, so we have to guess it from other parameters
314+
if peer.AwsAccountID != "" {
315+
if err := d.Set("provider_name", "AWS"); err != nil {
316+
return nil, fmt.Errorf("Error setting provider name: %v", err)
317+
}
318+
} else if peer.GcpProjectID != "" {
319+
if err := d.Set("provider_name", "GCP"); err != nil {
320+
return nil, fmt.Errorf("Error setting provider name: %v", err)
321+
}
322+
}
323+
303324
d.SetId(peer.ID)
304325
if err := d.Set("group", gid); err != nil {
305326
log.Printf("[WARN] Error setting group for (%s): %s", d.Id(), err)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package mongodbatlas
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/schema"
5+
)
6+
7+
func resourceVpcPeeringConnectionResourceV0() *schema.Resource {
8+
return &schema.Resource{
9+
Schema: map[string]*schema.Schema{
10+
"group": {
11+
Type: schema.TypeString,
12+
Required: true,
13+
ForceNew: true,
14+
},
15+
"route_table_cidr_block": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"aws_account_id": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
ForceNew: true,
23+
},
24+
"vpc_id": {
25+
Type: schema.TypeString,
26+
Required: true,
27+
ForceNew: true,
28+
},
29+
"container_id": {
30+
Type: schema.TypeString,
31+
Required: true,
32+
ForceNew: true,
33+
},
34+
"identifier": {
35+
Type: schema.TypeString,
36+
Optional: true,
37+
ForceNew: true,
38+
Computed: true,
39+
},
40+
"connection_id": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
ForceNew: true,
44+
Computed: true,
45+
},
46+
"status_name": {
47+
Type: schema.TypeString,
48+
Optional: true,
49+
Computed: true,
50+
},
51+
"error_state_name": {
52+
Type: schema.TypeString,
53+
Optional: true,
54+
Computed: true,
55+
},
56+
},
57+
}
58+
}
59+
60+
func resourceVpcPeeringConnectionStateUpgradeV0(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
61+
if v, ok := rawState["provider_name"]; (ok && v == "") || !ok {
62+
rawState["provider_name"] = "AWS"
63+
}
64+
65+
return rawState, nil
66+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package mongodbatlas
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func testResourceVpcPeeringConnectionResourceV0_aws() map[string]interface{} {
9+
return map[string]interface{}{
10+
"aws_account_id": "123456789",
11+
"connection_id": "pcx-123wefds43erg34ter",
12+
"container_id": "284nf7ek37g73jr7tj4jr8ei",
13+
"error_state_name": "",
14+
"group": "812nf72jf82j72j72hejw8yr",
15+
"id": "7s6b3r7tdsgf7t3igsdft3fu",
16+
"identifier": "7s6b3r7tdsgf7t3igsdft3fu",
17+
"route_table_cidr_block": "172.20.0.0/16",
18+
"status_name": "AVAILABLE",
19+
"vpc_id": "vpc-12345678",
20+
}
21+
}
22+
23+
func testResourceVpcPeeringConnectionResourceV1_aws() map[string]interface{} {
24+
v0 := testResourceVpcPeeringConnectionResourceV0_aws()
25+
return map[string]interface{}{
26+
"aws_account_id": v0["aws_account_id"],
27+
"connection_id": v0["connection_id"],
28+
"container_id": v0["container_id"],
29+
"error_state_name": v0["error_state_name"],
30+
"group": v0["group"],
31+
"id": v0["id"],
32+
"identifier": v0["identifier"],
33+
"route_table_cidr_block": v0["route_table_cidr_block"],
34+
"status_name": v0["status_name"],
35+
"vpc_id": v0["vpc_id"],
36+
"provider_name": "AWS",
37+
}
38+
}
39+
40+
func testResourceVpcPeeringConnectionResourceV0_gce() map[string]interface{} {
41+
return map[string]interface{}{
42+
"container_id": "507f1f77bcf86cd799439011",
43+
"error_message": "",
44+
"status": "ADDING_PEER",
45+
"gcp_project_id": "my-sample-project-191923",
46+
"network_name": "test1",
47+
"provider_name": "GCP",
48+
}
49+
}
50+
51+
func TestResourceAwsKinesisStreamStateUpgradeV0_aws(t *testing.T) {
52+
expected := testResourceVpcPeeringConnectionResourceV1_aws()
53+
actual, err := resourceVpcPeeringConnectionStateUpgradeV0(testResourceVpcPeeringConnectionResourceV0_aws(), nil)
54+
if err != nil {
55+
t.Fatalf("error migrating state: %s", err)
56+
}
57+
58+
if !reflect.DeepEqual(expected, actual) {
59+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
60+
}
61+
}
62+
63+
func TestResourceAwsKinesisStreamStateUpgradeV0_gce(t *testing.T) {
64+
expected := testResourceVpcPeeringConnectionResourceV0_gce()
65+
actual, err := resourceVpcPeeringConnectionStateUpgradeV0(testResourceVpcPeeringConnectionResourceV0_gce(), nil)
66+
if err != nil {
67+
t.Fatalf("error migrating state: %s", err)
68+
}
69+
70+
if !reflect.DeepEqual(expected, actual) {
71+
t.Fatalf("\n\nexpected:\n\n%#v\n\ngot:\n\n%#v\n\n", expected, actual)
72+
}
73+
}

0 commit comments

Comments
 (0)