-
Notifications
You must be signed in to change notification settings - Fork 670
/
data_source_ibm_pi_cloud_connections.go
192 lines (179 loc) · 6 KB
/
data_source_ibm_pi_cloud_connections.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package power
import (
"context"
"log"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
// Datasource to list Cloud Connections in a power instance
func DataSourceIBMPICloudConnections() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPICloudConnectionsRead,
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
// Attributes
Attr_Connections: {
Computed: true,
Description: "List of all the Cloud Connections.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_ClassicEnabled: {
Computed: true,
Description: "Enable classic endpoint destination.",
Type: schema.TypeBool,
},
Attr_CloudConnectionID: {
Computed: true,
Description: "The unique identifier of the cloud connection.",
Type: schema.TypeString,
},
Attr_ConnectionMode: {
Computed: true,
Description: "Type of service the gateway is attached to.",
Type: schema.TypeString,
},
Attr_GlobalRouting: {
Computed: true,
Description: "Enable global routing for this cloud connection.",
Type: schema.TypeBool,
},
Attr_GreDestinationAddress: {
Computed: true,
Description: "GRE destination IP address.",
Type: schema.TypeString,
},
Attr_GreSourceAddress: {
Computed: true,
Description: "GRE auto-assigned source IP address.",
Type: schema.TypeString,
},
Attr_IBMIPAddress: {
Computed: true,
Description: "IBM IP address.",
Type: schema.TypeString,
},
Attr_Metered: {
Computed: true,
Description: "Enable metering for this cloud connection.",
Type: schema.TypeBool,
},
Attr_Name: {
Computed: true,
Description: "Name of the cloud connection.",
Type: schema.TypeString,
},
Attr_Networks: {
Computed: true,
Description: "Set of Networks attached to this cloud connection.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
},
Attr_Port: {
Computed: true,
Description: "Port.",
Type: schema.TypeString,
},
Attr_Speed: {
Computed: true,
Description: "Speed of the cloud connection (speed in megabits per second).",
Type: schema.TypeInt,
},
Attr_Status: {
Computed: true,
Description: "Link status.",
Type: schema.TypeString,
},
Attr_UserIPAddress: {
Computed: true,
Description: "User IP address.",
Type: schema.TypeString,
},
Attr_VPCCRNs: {
Computed: true,
Description: "Set of VPCs attached to this cloud connection.",
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeSet,
},
Attr_VPCEnabled: {
Computed: true,
Description: "Enable VPC for this cloud connection.",
Type: schema.TypeBool,
},
},
},
Type: schema.TypeList,
},
},
}
}
func dataSourceIBMPICloudConnectionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
client := instance.NewIBMPICloudConnectionClient(ctx, sess, cloudInstanceID)
cloudConnections, err := client.GetAll()
if err != nil {
log.Printf("[DEBUG] get cloud connections failed %v", err)
return diag.FromErr(err)
}
result := make([]map[string]interface{}, 0, len(cloudConnections.CloudConnections))
for _, cloudConnection := range cloudConnections.CloudConnections {
cc := map[string]interface{}{
Attr_CloudConnectionID: *cloudConnection.CloudConnectionID,
Attr_ConnectionMode: cloudConnection.ConnectionMode,
Attr_GlobalRouting: *cloudConnection.GlobalRouting,
Attr_IBMIPAddress: *cloudConnection.IbmIPAddress,
Attr_Metered: *cloudConnection.Metered,
Attr_Name: *cloudConnection.Name,
Attr_Port: *cloudConnection.Port,
Attr_Speed: *cloudConnection.Speed,
Attr_Status: *cloudConnection.LinkStatus,
Attr_UserIPAddress: *cloudConnection.UserIPAddress,
}
if cloudConnection.Networks != nil {
networks := make([]string, len(cloudConnection.Networks))
for i, ccNetwork := range cloudConnection.Networks {
if ccNetwork != nil {
networks[i] = *ccNetwork.NetworkID
}
}
cc[Attr_Networks] = networks
}
if cloudConnection.Classic != nil {
cc[Attr_ClassicEnabled] = cloudConnection.Classic.Enabled
if cloudConnection.Classic.Gre != nil {
cc[Attr_GreDestinationAddress] = cloudConnection.Classic.Gre.DestIPAddress
cc[Attr_GreSourceAddress] = cloudConnection.Classic.Gre.SourceIPAddress
}
}
if cloudConnection.Vpc != nil {
cc[Attr_VPCEnabled] = cloudConnection.Vpc.Enabled
if cloudConnection.Vpc.Vpcs != nil && len(cloudConnection.Vpc.Vpcs) > 0 {
vpcCRNs := make([]string, len(cloudConnection.Vpc.Vpcs))
for i, vpc := range cloudConnection.Vpc.Vpcs {
vpcCRNs[i] = *vpc.VpcID
}
cc[Attr_VPCCRNs] = vpcCRNs
}
}
result = append(result, cc)
}
var genID, _ = uuid.GenerateUUID()
d.SetId(genID)
d.Set(Attr_Connections, result)
return nil
}