github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_is_ssh_key.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 // SSHKeyGenerator ... 28 type SSHKeyGenerator struct { 29 IBMService 30 } 31 32 func (g SSHKeyGenerator) createSSHKeyResources(sshKeyID, sshKeyName string) terraformutils.Resource { 33 resources := terraformutils.NewSimpleResource( 34 sshKeyID, 35 normalizeResourceName(sshKeyName, true), 36 "ibm_is_ssh_key", 37 "ibm", 38 []string{}) 39 return resources 40 } 41 42 // InitResources ... 43 func (g *SSHKeyGenerator) InitResources() error { 44 region := g.Args["region"].(string) 45 apiKey := os.Getenv("IC_API_KEY") 46 if apiKey == "" { 47 log.Fatal("No API key set") 48 } 49 50 vpcurl := fmt.Sprintf("https://%s.iaas.cloud.ibm.com/v1", region) 51 vpcoptions := &vpcv1.VpcV1Options{ 52 URL: envFallBack([]string{"IBMCLOUD_IS_API_ENDPOINT"}, vpcurl), 53 Authenticator: &core.IamAuthenticator{ 54 ApiKey: apiKey, 55 }, 56 } 57 vpcclient, err := vpcv1.NewVpcV1(vpcoptions) 58 if err != nil { 59 return err 60 } 61 options := &vpcv1.ListKeysOptions{} 62 if rg := g.Args["resource_group"].(string); rg != "" { 63 rg, err = GetResourceGroupID(apiKey, rg, region) 64 if err != nil { 65 return fmt.Errorf("Error Fetching Resource Group Id %s", err) 66 } 67 options.ResourceGroupID = &rg 68 } 69 keys, response, err := vpcclient.ListKeys(options) 70 if err != nil { 71 return fmt.Errorf("Error Fetching SSH Keys %s\n%s", err, response) 72 } 73 74 for _, key := range keys.Keys { 75 g.Resources = append(g.Resources, g.createSSHKeyResources(*key.ID, *key.Name)) 76 } 77 return nil 78 }