1
1
package servicediscovery
2
2
3
3
import (
4
+ "context"
5
+
4
6
"github.com/aws/aws-sdk-go/aws"
5
7
"github.com/aws/aws-sdk-go/service/servicediscovery"
6
8
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
7
9
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8
10
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
9
11
)
10
12
11
- func FindInstanceByServiceIDAndInstanceID (conn * servicediscovery.ServiceDiscovery , serviceID , instanceID string ) (* servicediscovery.Instance , error ) {
13
+ func FindInstanceByServiceIDAndInstanceID (ctx context. Context , conn * servicediscovery.ServiceDiscovery , serviceID , instanceID string ) (* servicediscovery.Instance , error ) {
12
14
input := & servicediscovery.GetInstanceInput {
13
15
InstanceId : aws .String (instanceID ),
14
16
ServiceId : aws .String (serviceID ),
15
17
}
16
18
17
- output , err := conn .GetInstance ( input )
19
+ output , err := conn .GetInstanceWithContext ( ctx , input )
18
20
19
21
if tfawserr .ErrCodeEquals (err , servicediscovery .ErrCodeInstanceNotFound ) {
20
22
return nil , & resource.NotFoundError {
@@ -34,12 +36,89 @@ func FindInstanceByServiceIDAndInstanceID(conn *servicediscovery.ServiceDiscover
34
36
return output .Instance , nil
35
37
}
36
38
37
- func FindOperationByID (conn * servicediscovery.ServiceDiscovery , id string ) (* servicediscovery.Operation , error ) {
39
+ func findNamespaces (ctx context.Context , conn * servicediscovery.ServiceDiscovery , input * servicediscovery.ListNamespacesInput ) ([]* servicediscovery.NamespaceSummary , error ) {
40
+ var output []* servicediscovery.NamespaceSummary
41
+
42
+ err := conn .ListNamespacesPagesWithContext (ctx , input , func (page * servicediscovery.ListNamespacesOutput , lastPage bool ) bool {
43
+ if page == nil {
44
+ return ! lastPage
45
+ }
46
+
47
+ for _ , v := range page .Namespaces {
48
+ if v != nil {
49
+ output = append (output , v )
50
+ }
51
+ }
52
+
53
+ return ! lastPage
54
+ })
55
+
56
+ if err != nil {
57
+ return nil , err
58
+ }
59
+
60
+ return output , nil
61
+ }
62
+
63
+ func findNamespacesByType (ctx context.Context , conn * servicediscovery.ServiceDiscovery , nsType string ) ([]* servicediscovery.NamespaceSummary , error ) {
64
+ input := & servicediscovery.ListNamespacesInput {
65
+ Filters : []* servicediscovery.NamespaceFilter {{
66
+ Condition : aws .String (servicediscovery .FilterConditionEq ),
67
+ Name : aws .String (servicediscovery .NamespaceFilterNameType ),
68
+ Values : aws .StringSlice ([]string {nsType }),
69
+ }},
70
+ }
71
+
72
+ return findNamespaces (ctx , conn , input )
73
+ }
74
+
75
+ func findNamespaceByNameAndType (ctx context.Context , conn * servicediscovery.ServiceDiscovery , name , nsType string ) (* servicediscovery.NamespaceSummary , error ) {
76
+ output , err := findNamespacesByType (ctx , conn , nsType )
77
+
78
+ if err != nil {
79
+ return nil , err
80
+ }
81
+
82
+ for _ , v := range output {
83
+ if aws .StringValue (v .Name ) == name {
84
+ return v , nil
85
+ }
86
+ }
87
+
88
+ return nil , & resource.NotFoundError {}
89
+ }
90
+
91
+ func FindNamespaceByID (ctx context.Context , conn * servicediscovery.ServiceDiscovery , id string ) (* servicediscovery.Namespace , error ) {
92
+ input := & servicediscovery.GetNamespaceInput {
93
+ Id : aws .String (id ),
94
+ }
95
+
96
+ output , err := conn .GetNamespaceWithContext (ctx , input )
97
+
98
+ if tfawserr .ErrCodeEquals (err , servicediscovery .ErrCodeNamespaceNotFound ) {
99
+ return nil , & resource.NotFoundError {
100
+ LastError : err ,
101
+ LastRequest : input ,
102
+ }
103
+ }
104
+
105
+ if err != nil {
106
+ return nil , err
107
+ }
108
+
109
+ if output == nil || output .Namespace == nil {
110
+ return nil , tfresource .NewEmptyResultError (input )
111
+ }
112
+
113
+ return output .Namespace , nil
114
+ }
115
+
116
+ func FindOperationByID (ctx context.Context , conn * servicediscovery.ServiceDiscovery , id string ) (* servicediscovery.Operation , error ) {
38
117
input := & servicediscovery.GetOperationInput {
39
118
OperationId : aws .String (id ),
40
119
}
41
120
42
- output , err := conn .GetOperation ( input )
121
+ output , err := conn .GetOperationWithContext ( ctx , input )
43
122
44
123
if tfawserr .ErrCodeEquals (err , servicediscovery .ErrCodeOperationNotFound ) {
45
124
return nil , & resource.NotFoundError {
@@ -59,12 +138,64 @@ func FindOperationByID(conn *servicediscovery.ServiceDiscovery, id string) (*ser
59
138
return output .Operation , nil
60
139
}
61
140
62
- func FindServiceByID (conn * servicediscovery.ServiceDiscovery , id string ) (* servicediscovery.Service , error ) {
141
+ func findServices (ctx context.Context , conn * servicediscovery.ServiceDiscovery , input * servicediscovery.ListServicesInput ) ([]* servicediscovery.ServiceSummary , error ) {
142
+ var output []* servicediscovery.ServiceSummary
143
+
144
+ err := conn .ListServicesPagesWithContext (ctx , input , func (page * servicediscovery.ListServicesOutput , lastPage bool ) bool {
145
+ if page == nil {
146
+ return ! lastPage
147
+ }
148
+
149
+ for _ , v := range page .Services {
150
+ if v != nil {
151
+ output = append (output , v )
152
+ }
153
+ }
154
+
155
+ return ! lastPage
156
+ })
157
+
158
+ if err != nil {
159
+ return nil , err
160
+ }
161
+
162
+ return output , nil
163
+ }
164
+
165
+ func findServicesByNamespaceID (ctx context.Context , conn * servicediscovery.ServiceDiscovery , namespaceID string ) ([]* servicediscovery.ServiceSummary , error ) {
166
+ input := & servicediscovery.ListServicesInput {
167
+ Filters : []* servicediscovery.ServiceFilter {{
168
+ Condition : aws .String (servicediscovery .FilterConditionEq ),
169
+ Name : aws .String (servicediscovery .ServiceFilterNameNamespaceId ),
170
+ Values : aws .StringSlice ([]string {namespaceID }),
171
+ }},
172
+ }
173
+
174
+ return findServices (ctx , conn , input )
175
+ }
176
+
177
+ func findServiceByNameAndNamespaceID (ctx context.Context , conn * servicediscovery.ServiceDiscovery , name , namespaceID string ) (* servicediscovery.ServiceSummary , error ) {
178
+ output , err := findServicesByNamespaceID (ctx , conn , namespaceID )
179
+
180
+ if err != nil {
181
+ return nil , err
182
+ }
183
+
184
+ for _ , v := range output {
185
+ if aws .StringValue (v .Name ) == name {
186
+ return v , nil
187
+ }
188
+ }
189
+
190
+ return nil , & resource.NotFoundError {}
191
+ }
192
+
193
+ func FindServiceByID (ctx context.Context , conn * servicediscovery.ServiceDiscovery , id string ) (* servicediscovery.Service , error ) {
63
194
input := & servicediscovery.GetServiceInput {
64
195
Id : aws .String (id ),
65
196
}
66
197
67
- output , err := conn .GetService ( input )
198
+ output , err := conn .GetServiceWithContext ( ctx , input )
68
199
69
200
if tfawserr .ErrCodeEquals (err , servicediscovery .ErrCodeServiceNotFound ) {
70
201
return nil , & resource.NotFoundError {
0 commit comments