github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_subnet.go (about) 1 // Copyright 2019 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 ibm 16 17 import ( 18 "fmt" 19 "os" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 "github.com/IBM/go-sdk-core/v4/core" 23 "github.com/IBM/vpc-go-sdk/vpcv1" 24 ) 25 26 // SubnetGenerator ... 27 type SubnetGenerator struct { 28 IBMService 29 } 30 31 func (g SubnetGenerator) createSubnetResources(subnetID, subnetName string) terraformutils.Resource { 32 resource := terraformutils.NewResource( 33 subnetID, 34 normalizeResourceName(subnetName, false), 35 "ibm_is_subnet", 36 "ibm", 37 map[string]string{}, 38 []string{}, 39 map[string]interface{}{}) 40 41 resource.IgnoreKeys = append(resource.IgnoreKeys, 42 "^total_ipv4_address_count$", 43 ) 44 return resource 45 } 46 47 // InitResources ... 48 func (g *SubnetGenerator) InitResources() error { 49 region := g.Args["region"].(string) 50 apiKey := os.Getenv("IC_API_KEY") 51 if apiKey == "" { 52 return fmt.Errorf("No API key set") 53 } 54 55 vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region) 56 vpcoptions := &vpcv1.VpcV1Options{ 57 URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl), 58 Authenticator: &core.IamAuthenticator{ 59 ApiKey: apiKey, 60 }, 61 } 62 vpcclient, err := vpcv1.NewVpcV1(vpcoptions) 63 if err != nil { 64 return err 65 } 66 67 start := "" 68 var allrecs []vpcv1.Subnet 69 for { 70 options := &vpcv1.ListSubnetsOptions{} 71 if start != "" { 72 options.Start = &start 73 } 74 if rg := g.Args["resource_group"].(string); rg != "" { 75 rg, err = GetResourceGroupID(apiKey, rg, region) 76 if err != nil { 77 return fmt.Errorf("Error Fetching Resource Group Id %s", err) 78 } 79 options.ResourceGroupID = &rg 80 } 81 subnets, response, err := vpcclient.ListSubnets(options) 82 if err != nil { 83 return fmt.Errorf("Error Fetching subnets %s\n%s", err, response) 84 } 85 start = GetNext(subnets.Next) 86 allrecs = append(allrecs, subnets.Subnets...) 87 if start == "" { 88 break 89 } 90 } 91 92 for _, subnet := range allrecs { 93 g.Resources = append(g.Resources, g.createSubnetResources(*subnet.ID, *subnet.Name)) 94 } 95 return nil 96 }