@@ -20,11 +20,11 @@ const (
2020// ServiceDiscoveryApi handles the AWS Cloud Map API request and response processing logic, and converts results to
2121// internal data structures. It manages all interactions with the AWS SDK.
2222type ServiceDiscoveryApi interface {
23- // ListNamespaces returns a list of all namespaces.
24- ListNamespaces (ctx context.Context ) (namespaces [ ]* model.Namespace , err error )
23+ // GetNamespaceMap returns a map of all namespaces in the Cloud Map account indexed by namespace name .
24+ GetNamespaceMap (ctx context.Context ) (namespaces map [ string ]* model.Namespace , err error )
2525
26- // ListServices returns a list of services for a given namespace.
27- ListServices (ctx context.Context , namespaceId string ) (services [] * model. Resource , err error )
26+ // GetServiceIdMap returns a map of all service IDs for a given namespace indexed by service name .
27+ GetServiceIdMap (ctx context.Context , namespaceId string ) (serviceIdMap map [ string ] string , err error )
2828
2929 // DiscoverInstances returns a list of service instances registered to a given service.
3030 DiscoverInstances (ctx context.Context , nsName string , svcName string ) (insts []types.HttpInstanceSummary , err error )
@@ -64,52 +64,53 @@ func NewServiceDiscoveryApiFromConfig(cfg *aws.Config) ServiceDiscoveryApi {
6464 }
6565}
6666
67- func (sdApi * serviceDiscoveryApi ) ListNamespaces (ctx context.Context ) (namespaces [ ]* model.Namespace , err error ) {
68- pages := sd . NewListNamespacesPaginator ( sdApi . awsFacade , & sd. ListNamespacesInput {} )
67+ func (sdApi * serviceDiscoveryApi ) GetNamespaceMap (ctx context.Context ) (map [ string ]* model.Namespace , error ) {
68+ namespaceMap := make ( map [ string ] * model. Namespace )
6969
70+ pages := sd .NewListNamespacesPaginator (sdApi .awsFacade , & sd.ListNamespacesInput {})
7071 for pages .HasMorePages () {
7172 output , err := pages .NextPage (ctx )
7273 if err != nil {
73- return namespaces , err
74+ return nil , err
7475 }
7576
7677 for _ , ns := range output .Namespaces {
77- if namespaceType := model .ConvertNamespaceType (ns .Type ); ! namespaceType .IsUnsupported () {
78- namespaces = append (namespaces , & model.Namespace {
79- Id : aws .ToString (ns .Id ),
80- Name : aws .ToString (ns .Name ),
81- Type : namespaceType ,
82- })
78+ namespaceType := model .ConvertNamespaceType (ns .Type )
79+ if namespaceType .IsUnsupported () {
80+ continue
81+ }
82+ namespaceMap [aws .ToString (ns .Name )] = & model.Namespace {
83+ Id : aws .ToString (ns .Id ),
84+ Name : aws .ToString (ns .Name ),
85+ Type : namespaceType ,
8386 }
8487 }
8588 }
8689
87- return namespaces , nil
90+ return namespaceMap , nil
8891}
8992
90- func (sdApi * serviceDiscoveryApi ) ListServices (ctx context.Context , nsId string ) (svcs []* model.Resource , err error ) {
93+ func (sdApi * serviceDiscoveryApi ) GetServiceIdMap (ctx context.Context , nsId string ) (map [string ]string , error ) {
94+ serviceIdMap := make (map [string ]string )
95+
9196 filter := types.ServiceFilter {
9297 Name : types .ServiceFilterNameNamespaceId ,
9398 Values : []string {nsId },
9499 }
95100
96101 pages := sd .NewListServicesPaginator (sdApi .awsFacade , & sd.ListServicesInput {Filters : []types.ServiceFilter {filter }})
97-
98102 for pages .HasMorePages () {
99103 output , err := pages .NextPage (ctx )
100104 if err != nil {
101- return svcs , err
105+ return nil , err
102106 }
103107
104108 for _ , svc := range output .Services {
105- svcs = append (svcs , & model.Resource {
106- Id : aws .ToString (svc .Id ),
107- Name : aws .ToString (svc .Name ),
108- })
109+ serviceIdMap [aws .ToString (svc .Name )] = aws .ToString (svc .Id )
109110 }
110111 }
111112
112- return svcs , nil
113+ return serviceIdMap , nil
113114}
114115
115116func (sdApi * serviceDiscoveryApi ) DiscoverInstances (ctx context.Context , nsName string , svcName string ) (insts []types.HttpInstanceSummary , err error ) {
@@ -127,8 +128,8 @@ func (sdApi *serviceDiscoveryApi) DiscoverInstances(ctx context.Context, nsName
127128 return out .Instances , nil
128129}
129130
130- func (sdApi * serviceDiscoveryApi ) ListOperations (ctx context.Context , opFilters []types.OperationFilter ) (opStatusMap map [string ]types.OperationStatus , err error ) {
131- opStatusMap = make (map [string ]types.OperationStatus )
131+ func (sdApi * serviceDiscoveryApi ) ListOperations (ctx context.Context , opFilters []types.OperationFilter ) (map [string ]types.OperationStatus , error ) {
132+ opStatusMap : = make (map [string ]types.OperationStatus )
132133
133134 pages := sd .NewListOperationsPaginator (sdApi .awsFacade , & sd.ListOperationsInput {
134135 Filters : opFilters ,
@@ -190,7 +191,7 @@ func (sdApi *serviceDiscoveryApi) CreateService(ctx context.Context, namespace m
190191 }
191192
192193 svcId = aws .ToString (output .Service .Id )
193- sdApi .log .Info ("service created" , "svcId " , svcId )
194+ sdApi .log .Info ("service created" , "namespace" , namespace . Name , "name" , svcName , "id " , svcId )
194195 return svcId , nil
195196}
196197
0 commit comments