github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/nat_gateway.go (about) 1 // Copyright 2018 The Terraformer Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package alicloud 16 17 import ( 18 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 19 "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests" 20 "github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" 21 ) 22 23 // NatGatewayGenerator Struct for generating AliCloud Elastic Compute Service 24 type NatGatewayGenerator struct { 25 AliCloudService 26 } 27 28 func resourceFromNatGatewayResponse(natGateway vpc.NatGateway) terraformutils.Resource { 29 return terraformutils.NewResource( 30 natGateway.NatGatewayId, // id 31 natGateway.NatGatewayId+"__"+natGateway.Name, // name 32 "alicloud_nat_gateway", 33 "alicloud", 34 map[string]string{}, 35 []string{}, 36 map[string]interface{}{}, 37 ) 38 } 39 40 // InitResources Gets the list of all natgateway NatGateway ids and generates resources 41 func (g *NatGatewayGenerator) InitResources() error { 42 client, err := g.LoadClientFromProfile() 43 if err != nil { 44 return err 45 } 46 remaining := 1 47 pageNumber := 1 48 pageSize := 10 49 50 allNatGateways := make([]vpc.NatGateway, 0) 51 52 for remaining > 0 { 53 raw, err := client.WithVpcClient(func(vpcClient *vpc.Client) (interface{}, error) { 54 request := vpc.CreateDescribeNatGatewaysRequest() 55 request.RegionId = client.RegionID 56 request.PageSize = requests.NewInteger(pageSize) 57 request.PageNumber = requests.NewInteger(pageNumber) 58 return vpcClient.DescribeNatGateways(request) 59 }) 60 if err != nil { 61 return err 62 } 63 64 response := raw.(*vpc.DescribeNatGatewaysResponse) 65 allNatGateways = append(allNatGateways, response.NatGateways.NatGateway...) 66 remaining = response.TotalCount - pageNumber*pageSize 67 pageNumber++ 68 } 69 70 for _, NatGateway := range allNatGateways { 71 resource := resourceFromNatGatewayResponse(NatGateway) 72 g.Resources = append(g.Resources, resource) 73 } 74 75 return nil 76 }