This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
/
data_source_google_compute_region_instance_group.go
146 lines (130 loc) · 3.53 KB
/
data_source_google_compute_region_instance_group.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
package google
import (
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/googleapi"
)
func dataSourceGoogleComputeRegionInstanceGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceComputeRegionInstanceGroupRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"instances": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance": {
Type: schema.TypeString,
Required: true,
},
"status": {
Type: schema.TypeString,
Required: true,
},
"named_ports": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"port": {
Type: schema.TypeInt,
Required: true,
},
},
},
},
},
},
},
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"self_link": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"size": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func dataSourceComputeRegionInstanceGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, region, name, err := GetRegionalResourcePropertiesFromSelfLinkOrSchema(d, config)
if err != nil {
return err
}
instanceGroup, err := config.clientCompute.RegionInstanceGroups.Get(
project, region, name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Region Instance Group %q", name))
}
members, err := config.clientCompute.RegionInstanceGroups.ListInstances(
project, region, name, &compute.RegionInstanceGroupsListInstancesRequest{
InstanceState: "ALL",
}).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
// The resource doesn't have any instances, which is okay.
d.Set("instances", nil)
} else {
return fmt.Errorf("Error reading RegionInstanceGroup Members: %s", err)
}
} else {
d.Set("instances", flattenInstancesWithNamedPorts(members.Items))
}
d.SetId(strconv.FormatUint(instanceGroup.Id, 16))
d.Set("self_link", instanceGroup.SelfLink)
d.Set("name", name)
d.Set("project", project)
d.Set("region", region)
return nil
}
func flattenInstancesWithNamedPorts(insts []*compute.InstanceWithNamedPorts) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(insts))
log.Printf("There were %d instances.\n", len(insts))
for _, inst := range insts {
instMap := make(map[string]interface{})
instMap["instance"] = inst.Instance
instMap["named_ports"] = flattenNamedPorts(inst.NamedPorts)
instMap["status"] = inst.Status
result = append(result, instMap)
}
return result
}
func flattenNamedPorts(namedPorts []*compute.NamedPort) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(namedPorts))
for _, namedPort := range namedPorts {
namedPortMap := make(map[string]interface{})
namedPortMap["name"] = namedPort.Name
namedPortMap["port"] = namedPort.Port
result = append(result, namedPortMap)
}
return result
}