github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/ibm_certificate_manager.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 "os" 19 20 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 21 bluemix "github.com/IBM-Cloud/bluemix-go" 22 "github.com/IBM-Cloud/bluemix-go/api/certificatemanager" 23 "github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/catalog" 24 "github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2" 25 "github.com/IBM-Cloud/bluemix-go/session" 26 ) 27 28 type CMGenerator struct { 29 IBMService 30 } 31 32 func (g CMGenerator) loadCM(cmID, cmGuID string) terraformutils.Resource { 33 resources := terraformutils.NewSimpleResource( 34 cmID, 35 cmGuID, 36 "ibm_resource_instance", 37 "ibm", 38 []string{}) 39 return resources 40 } 41 42 func (g CMGenerator) loadImportedCM(cmID, certificateID, cisInstance string, dependsOn []string) terraformutils.Resource { 43 resources := terraformutils.NewResource( 44 cmID, 45 certificateID, 46 "ibm_certificate_manager_import", 47 "ibm", 48 map[string]string{ 49 "dns_provider_instance_crn": cisInstance, 50 }, 51 []string{}, 52 map[string]interface{}{ 53 "depends_on": dependsOn, 54 }) 55 return resources 56 } 57 58 func (g CMGenerator) loadOrderedCM(cmID, certificateID, cisInstance string, dependsOn []string) terraformutils.Resource { 59 resources := terraformutils.NewResource( 60 cmID, 61 certificateID, 62 "ibm_certificate_manager_order", 63 "ibm", 64 map[string]string{ 65 "dns_provider_instance_crn": cisInstance, 66 }, 67 []string{}, 68 map[string]interface{}{ 69 "depends_on": dependsOn, 70 }) 71 return resources 72 } 73 74 func (g *CMGenerator) InitResources() error { 75 76 bmxConfig := &bluemix.Config{ 77 BluemixAPIKey: os.Getenv("IC_API_KEY"), 78 } 79 sess, err := session.New(bmxConfig) 80 if err != nil { 81 return err 82 } 83 84 var cisInstance string 85 var cisID string 86 cis := g.Args["cis"] 87 if cis != nil { 88 cisInstance = cis.(string) 89 } 90 91 // Client creation 92 catalogClient, err := catalog.New(sess) 93 if err != nil { 94 return err 95 } 96 97 controllerClient, err := controllerv2.New(sess) 98 if err != nil { 99 return err 100 } 101 102 certManagementClient, err := certificatemanager.New(sess) 103 if err != nil { 104 return err 105 } 106 107 // Get ServiceID of certificate manager service 108 serviceID, err := catalogClient.ResourceCatalog().FindByName("cloudcerts", true) 109 if err != nil { 110 return err 111 } 112 113 serviceID2, err := catalogClient.ResourceCatalog().FindByName("internet-svcs", true) 114 if err != nil { 115 return err 116 } 117 118 query := controllerv2.ServiceInstanceQuery{ 119 ServiceID: serviceID[0].ID, 120 } 121 122 query2 := controllerv2.ServiceInstanceQuery{ 123 ServiceID: serviceID2[0].ID, 124 } 125 126 // Get all Certificate manager instances 127 cmInstances, err := controllerClient.ResourceServiceInstanceV2().ListInstances(query) 128 if err != nil { 129 return err 130 } 131 132 // Get all CIS instances 133 cisInstances, err := controllerClient.ResourceServiceInstanceV2().ListInstances(query2) 134 if err != nil { 135 return err 136 } 137 for _, cis := range cisInstances { 138 if cisInstance == cis.Name { 139 cisID = cis.Guid 140 } 141 } 142 143 // Get all certificates associated with a certificate manager instance 144 for _, cmInstance := range cmInstances { 145 146 g.Resources = append(g.Resources, g.loadCM(cmInstance.ID, cmInstance.Guid)) 147 148 // For each instance get associated certificates 149 certificateList, err := certManagementClient.Certificate().ListCertificates(cmInstance.ID) 150 if err != nil { 151 return err 152 } 153 154 for _, cert := range certificateList { 155 // Get certificate info 156 certificatedata, err := certManagementClient.Certificate().GetCertData(cert.ID) 157 if err != nil { 158 return err 159 } 160 161 var dependsOn []string 162 dependsOn = append(dependsOn, 163 "ibm_resource_instance."+terraformutils.TfSanitize(cmInstance.Guid)) 164 165 if certificatedata.Imported { 166 g.Resources = append(g.Resources, g.loadImportedCM(cert.ID, cert.ID, cisID, dependsOn)) 167 } else { 168 g.Resources = append(g.Resources, g.loadOrderedCM(cert.ID, cert.ID, cisID, dependsOn)) 169 } 170 } 171 } 172 173 return nil 174 }