@@ -2,6 +2,7 @@ package cloudmap
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
"github.com/aws/aws-cloud-map-mcs-controller-for-k8s/pkg/model"
7
8
"github.com/aws/aws-sdk-go-v2/aws"
@@ -16,10 +17,10 @@ import (
16
17
// internal data structures. It manages all interactions with the AWS SDK.
17
18
type ServiceDiscoveryApi interface {
18
19
// ListNamespaces returns a list of all namespaces.
19
- ListNamespaces (ctx context.Context ) (namespaces []* Resource , err error )
20
+ ListNamespaces (ctx context.Context ) (namespaces []* model. Resource , err error )
20
21
21
22
// ListServices returns a list of services for a given namespace.
22
- ListServices (ctx context.Context , namespaceId string ) (services []* Resource , err error )
23
+ ListServices (ctx context.Context , namespaceId string ) (services []* model. Resource , err error )
23
24
24
25
// ListInstances returns a list of service instances registered to a given service.
25
26
ListInstances (ctx context.Context , serviceId string ) ([]* model.Endpoint , error )
@@ -51,12 +52,6 @@ type serviceDiscoveryApi struct {
51
52
awsFacade AwsFacade
52
53
}
53
54
54
- // Resource encapsulates a ID/name pair
55
- type Resource struct {
56
- Id string
57
- Name string
58
- }
59
-
60
55
// NewServiceDiscoveryApiFromConfig creates a new AWS Cloud Map API connection manager from an AWS client config.
61
56
func NewServiceDiscoveryApiFromConfig (cfg * aws.Config ) ServiceDiscoveryApi {
62
57
return & serviceDiscoveryApi {
@@ -65,8 +60,8 @@ func NewServiceDiscoveryApiFromConfig(cfg *aws.Config) ServiceDiscoveryApi {
65
60
}
66
61
}
67
62
68
- func (sdApi * serviceDiscoveryApi ) ListNamespaces (ctx context.Context ) ([]* Resource , error ) {
69
- namespaces := make ([]* Resource , 0 )
63
+ func (sdApi * serviceDiscoveryApi ) ListNamespaces (ctx context.Context ) ([]* model. Resource , error ) {
64
+ namespaces := make ([]* model. Resource , 0 )
70
65
pages := sd .NewListNamespacesPaginator (sdApi .awsFacade , & sd.ListNamespacesInput {})
71
66
72
67
for pages .HasMorePages () {
@@ -76,7 +71,7 @@ func (sdApi *serviceDiscoveryApi) ListNamespaces(ctx context.Context) ([]*Resour
76
71
}
77
72
78
73
for _ , ns := range output .Namespaces {
79
- namespaces = append (namespaces , & Resource {
74
+ namespaces = append (namespaces , & model. Resource {
80
75
Id : aws .ToString (ns .Id ),
81
76
Name : aws .ToString (ns .Name ),
82
77
})
@@ -86,8 +81,8 @@ func (sdApi *serviceDiscoveryApi) ListNamespaces(ctx context.Context) ([]*Resour
86
81
return namespaces , nil
87
82
}
88
83
89
- func (sdApi * serviceDiscoveryApi ) ListServices (ctx context.Context , nsId string ) ([]* Resource , error ) {
90
- svcs := make ([]* Resource , 0 )
84
+ func (sdApi * serviceDiscoveryApi ) ListServices (ctx context.Context , nsId string ) ([]* model. Resource , error ) {
85
+ svcs := make ([]* model. Resource , 0 )
91
86
92
87
filter := types.ServiceFilter {
93
88
Name : types .ServiceFilterNameNamespaceId ,
@@ -103,7 +98,7 @@ func (sdApi *serviceDiscoveryApi) ListServices(ctx context.Context, nsId string)
103
98
}
104
99
105
100
for _ , svc := range output .Services {
106
- svcs = append (svcs , & Resource {
101
+ svcs = append (svcs , & model. Resource {
107
102
Id : aws .ToString (svc .Id ),
108
103
Name : aws .ToString (svc .Name ),
109
104
})
@@ -226,12 +221,12 @@ func (sdApi *serviceDiscoveryApi) DeregisterInstance(ctx context.Context, svcId
226
221
}
227
222
228
223
func (sdApi * serviceDiscoveryApi ) PollCreateNamespace (ctx context.Context , opId string ) (nsId string , err error ) {
229
- return nsId , wait .Poll (defaultOperationPollInterval , defaultOperationPollTimeout , func () (done bool , pollErr error ) {
224
+ err = wait .Poll (defaultOperationPollInterval , defaultOperationPollTimeout , func () (done bool , err error ) {
230
225
sdApi .log .Info ("polling operation" , "opId" , opId )
231
- op , opErr := sdApi .GetOperation (ctx , opId )
226
+ op , err := sdApi .GetOperation (ctx , opId )
232
227
233
- if opErr != nil {
234
- return true , opErr
228
+ if err != nil {
229
+ return true , err
235
230
}
236
231
237
232
if op .Status == types .OperationStatusFail {
@@ -246,4 +241,10 @@ func (sdApi *serviceDiscoveryApi) PollCreateNamespace(ctx context.Context, opId
246
241
247
242
return false , nil
248
243
})
244
+
245
+ if err == wait .ErrWaitTimeout {
246
+ err = errors .New (operationPollTimoutErrorMessage )
247
+ }
248
+
249
+ return nsId , err
249
250
}
0 commit comments