github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/ibm/database_postgresql.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  	"github.com/IBM-Cloud/bluemix-go"
    22  	"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev1/catalog"
    23  	"github.com/IBM-Cloud/bluemix-go/api/resource/resourcev2/controllerv2"
    24  	"github.com/IBM-Cloud/bluemix-go/session"
    25  )
    26  
    27  // DatabasePostgresqlGenerator ...
    28  type DatabasePostgresqlGenerator struct {
    29  	IBMService
    30  }
    31  
    32  // loadPostgresqlDB ...
    33  func (g DatabasePostgresqlGenerator) loadPostgresqlDB(dbID string, dbName string) terraformutils.Resource {
    34  	resources := terraformutils.NewSimpleResource(
    35  		dbID,
    36  		normalizeResourceName(dbName, false),
    37  		"ibm_database",
    38  		"ibm",
    39  		[]string{})
    40  	return resources
    41  }
    42  
    43  // InitResources ...
    44  func (g *DatabasePostgresqlGenerator) InitResources() error {
    45  	region := g.Args["region"].(string)
    46  	bmxConfig := &bluemix.Config{
    47  		BluemixAPIKey: os.Getenv("IC_API_KEY"),
    48  		Region:        region,
    49  	}
    50  	sess, err := session.New(bmxConfig)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	catalogClient, err := catalog.New(sess)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	controllerClient, err := controllerv2.New(sess)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	serviceID, err := catalogClient.ResourceCatalog().FindByName("databases-for-postgresql", true)
    66  	if err != nil {
    67  		return err
    68  	}
    69  	query := controllerv2.ServiceInstanceQuery{
    70  		ServiceID: serviceID[0].ID,
    71  	}
    72  	postgreSQLInstances, err := controllerClient.ResourceServiceInstanceV2().ListInstances(query)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	for _, db := range postgreSQLInstances {
    78  		if db.RegionID == region {
    79  			g.Resources = append(g.Resources, g.loadPostgresqlDB(db.ID, db.Name))
    80  		}
    81  
    82  	}
    83  
    84  	return nil
    85  }