This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_opentelekomcloud_vpc_peering_connection_v2.go
96 lines (82 loc) · 2.33 KB
/
data_source_opentelekomcloud_vpc_peering_connection_v2.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
package opentelekomcloud
import (
"fmt"
"log"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/peerings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func dataSourceVpcPeeringConnectionV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceVpcPeeringConnectionV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateName,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"peer_vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"peer_tenant_id": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceVpcPeeringConnectionV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
peeringClient, err := config.hwNetworkV2Client(GetRegion(d, config))
if err != nil {
return err
}
listOpts := peerings.ListOpts{
ID: d.Id(),
Name: d.Get("name").(string),
Status: d.Get("status").(string),
TenantId: d.Get("peer_tenant_id").(string),
VpcId: d.Get("vpc_id").(string),
Peer_VpcId: d.Get("peer_vpc_id").(string),
}
refinedPeering, err := peerings.List(peeringClient, listOpts)
if err != nil {
return fmt.Errorf("Unable to retrieve vpc peering connections: %s", err)
}
if len(refinedPeering) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedPeering) > 1 {
return fmt.Errorf("Multiple VPC peering connections matched." +
" Use additional constraints to reduce matches to a single VPC peering connection")
}
Peering := refinedPeering[0]
log.Printf("[INFO] Retrieved Vpc peering Connections using given filter %s: %+v", Peering.ID, Peering)
d.SetId(Peering.ID)
d.Set("id", Peering.ID)
d.Set("name", Peering.Name)
d.Set("status", Peering.Status)
d.Set("vpc_id", Peering.RequestVpcInfo.VpcId)
d.Set("peer_vpc_id", Peering.AcceptVpcInfo.VpcId)
d.Set("peer_tenant_id", Peering.AcceptVpcInfo.TenantId)
d.Set("region", GetRegion(d, config))
return nil
}