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