@@ -129,11 +129,11 @@ func (b *ecsAPIService) parse(ctx context.Context, project *types.Project, templ
129129 if err != nil {
130130 return r , err
131131 }
132- r . vpc , r . subnets , err = b .parseVPCExtension (ctx , project )
132+ err = b .parseLoadBalancerExtension (ctx , project , & r )
133133 if err != nil {
134134 return r , err
135135 }
136- r . loadBalancer , r . loadBalancerType , err = b .parseLoadBalancerExtension (ctx , project )
136+ err = b .parseVPCExtension (ctx , project , & r )
137137 if err != nil {
138138 return r , err
139139 }
@@ -165,7 +165,7 @@ func (b *ecsAPIService) parseClusterExtension(ctx context.Context, project *type
165165 return nil , nil
166166}
167167
168- func (b * ecsAPIService ) parseVPCExtension (ctx context.Context , project * types.Project ) ( string , [] awsResource , error ) {
168+ func (b * ecsAPIService ) parseVPCExtension (ctx context.Context , project * types.Project , r * awsResources ) error {
169169 var vpc string
170170 if x , ok := project .Extensions [extensionVPC ]; ok {
171171 vpc = x .(string )
@@ -177,57 +177,75 @@ func (b *ecsAPIService) parseVPCExtension(ctx context.Context, project *types.Pr
177177 vpc = id [i + 1 :]
178178 }
179179
180+ if r .vpc != "" {
181+ if r .vpc != vpc {
182+ return fmt .Errorf ("load balancer set by %s is attached to VPC %s" , extensionLoadBalancer , r .vpc )
183+ }
184+ return nil
185+ }
186+
180187 err = b .aws .CheckVPC (ctx , vpc )
181188 if err != nil {
182- return "" , nil , err
189+ return err
183190 }
184191
185192 } else {
193+ if r .vpc != "" {
194+ return nil
195+ }
196+
186197 defaultVPC , err := b .aws .GetDefaultVPC (ctx )
187198 if err != nil {
188- return "" , nil , err
199+ return err
189200 }
190201 vpc = defaultVPC
191202 }
192203
193204 subNets , err := b .aws .GetSubNets (ctx , vpc )
194205 if err != nil {
195- return "" , nil , err
206+ return err
196207 }
197208
198209 var publicSubNets []awsResource
199210 for _ , subNet := range subNets {
200- isPublic , err := b .aws .IsPublicSubnet (ctx , vpc , subNet .ID ())
211+ isPublic , err := b .aws .IsPublicSubnet (ctx , subNet .ID ())
201212 if err != nil {
202- return "" , nil , err
213+ return err
203214 }
204215 if isPublic {
205216 publicSubNets = append (publicSubNets , subNet )
206217 }
207218 }
208219
209220 if len (publicSubNets ) < 2 {
210- return "" , nil , fmt .Errorf ("VPC %s should have at least 2 associated public subnets in different availability zones" , vpc )
221+ return fmt .Errorf ("VPC %s should have at least 2 associated public subnets in different availability zones" , vpc )
211222 }
212- return vpc , publicSubNets , nil
223+
224+ r .vpc = vpc
225+ r .subnets = subNets
226+ return nil
213227}
214228
215- func (b * ecsAPIService ) parseLoadBalancerExtension (ctx context.Context , project * types.Project ) ( awsResource , string , error ) {
229+ func (b * ecsAPIService ) parseLoadBalancerExtension (ctx context.Context , project * types.Project , r * awsResources ) error {
216230 if x , ok := project .Extensions [extensionLoadBalancer ]; ok {
217231 nameOrArn := x .(string )
218- loadBalancer , loadBalancerType , err := b .aws .ResolveLoadBalancer (ctx , nameOrArn )
232+ loadBalancer , loadBalancerType , vpc , subnets , err := b .aws .ResolveLoadBalancer (ctx , nameOrArn )
219233 if err != nil {
220- return nil , "" , err
234+ return err
221235 }
222236
223237 required := getRequiredLoadBalancerType (project )
224238 if loadBalancerType != required {
225- return nil , "" , fmt .Errorf ("load balancer %q is of type %s, project require a %s" , nameOrArn , loadBalancerType , required )
239+ return fmt .Errorf ("load balancer %q is of type %s, project require a %s" , nameOrArn , loadBalancerType , required )
226240 }
227241
228- return loadBalancer , loadBalancerType , err
242+ r .loadBalancer = loadBalancer
243+ r .loadBalancerType = loadBalancerType
244+ r .vpc = vpc
245+ r .subnets = subnets
246+ return err
229247 }
230- return nil , "" , nil
248+ return nil
231249}
232250
233251func (b * ecsAPIService ) parseExternalNetworks (ctx context.Context , project * types.Project ) (map [string ]string , error ) {
0 commit comments