This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
forked from oracle/terraform-provider-oci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity_availability_domain_data_source.go
145 lines (114 loc) · 3.06 KB
/
identity_availability_domain_data_source.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
// Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
package provider
import (
"context"
"sort"
"fmt"
"regexp"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
oci_identity "github.com/oracle/oci-go-sdk/identity"
)
func IdentityAvailabilityDomainDataSource() *schema.Resource {
return &schema.Resource{
Read: readAvailabilityDomain,
Schema: map[string]*schema.Schema{
"compartment_id": {
Type: schema.TypeString,
Required: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"ad_number": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
ConflictsWith: []string{"id"},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func readAvailabilityDomain(d *schema.ResourceData, m interface{}) error {
sync := &AvailabilityDomainDataSourceCrud{}
sync.D = d
sync.Client = m.(*OracleClients).identityClient
return ReadResource(sync)
}
type AvailabilityDomainDataSourceCrud struct {
D *schema.ResourceData
Client *oci_identity.IdentityClient
Res *oci_identity.ListAvailabilityDomainsResponse
}
func (s *AvailabilityDomainDataSourceCrud) VoidState() {
s.D.SetId("")
}
func (s *AvailabilityDomainDataSourceCrud) Get() error {
request := oci_identity.ListAvailabilityDomainsRequest{}
if compartmentId, ok := s.D.GetOkExists("compartment_id"); ok {
tmp := compartmentId.(string)
request.CompartmentId = &tmp
}
request.RequestMetadata.RetryPolicy = getRetryPolicy(false, "identity")
response, err := s.Client.ListAvailabilityDomains(context.Background(), request)
if err != nil {
return err
}
s.Res = &response
return nil
}
func (s *AvailabilityDomainDataSourceCrud) SetData() error {
if s.Res == nil {
return nil
}
items := s.Res.Items
// sort ADs by name
sort.Slice(items, func(i, j int) bool {
return *items[i].Name < *items[j].Name
})
id, idOk := s.D.GetOkExists("id")
adNumber, adNumberOk := s.D.GetOkExists("ad_number")
for _, r := range items {
adNumberFromName := adNumberFromName(*r.Name)
if adNumberFromName == -1 {
return fmt.Errorf("unable to get index from Availability Domain name pattern: %s", *r.Name)
}
// match on user supplied AD index or Availability Domain global OCID
if adNumberOk && adNumber == adNumberFromName || idOk && id == *r.Id {
s.D.SetId(*r.Id)
if r.CompartmentId != nil {
s.D.Set("compartment_id", *r.CompartmentId)
}
if r.Name != nil {
s.D.Set("name", *r.Name)
}
s.D.Set("ad_number", adNumberFromName)
return nil
}
}
if idOk {
return fmt.Errorf("no Availability Domain match for ID: %s", id)
} else {
return fmt.Errorf("no Availability Domain match for AD number: %d", adNumber)
}
}
func adNumberFromName(adName string) int {
regex := regexp.MustCompile(`AD-(\d)`)
res := regex.FindAllStringSubmatch(adName, -1)
// no matching AD name
if res == nil || len(res) < 1 {
return -1
}
// int coercion failure
index, err := strconv.Atoi(res[0][1])
if err != nil {
return -1
}
return index
}