github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/cloudsql.go (about)

     1  // Copyright 2018 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 gcp
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  
    22  	sqladmin "google.golang.org/api/sqladmin/v1beta4"
    23  )
    24  
    25  var cloudSQLAllowEmptyValues = []string{}
    26  
    27  var cloudSQLAdditionalFields = map[string]interface{}{}
    28  
    29  type CloudSQLGenerator struct {
    30  	GCPService
    31  }
    32  
    33  func (g *CloudSQLGenerator) loadDBInstances(svc *sqladmin.Service, project string) error {
    34  	dbInstances, err := svc.Instances.List(project).Do()
    35  	if err != nil {
    36  		return err
    37  	}
    38  	for _, dbInstance := range dbInstances.Items {
    39  		g.Resources = append(g.Resources, terraformutils.NewResource(
    40  			dbInstance.Name,
    41  			dbInstance.Name,
    42  			"google_sql_database_instance",
    43  			g.ProviderName,
    44  			map[string]string{
    45  				"project": project,
    46  				"name":    dbInstance.Name,
    47  			},
    48  			cloudSQLAllowEmptyValues,
    49  			cloudSQLAdditionalFields,
    50  		))
    51  		err := g.loadDBs(svc, dbInstance.Name, project)
    52  		if err != nil {
    53  			return err
    54  		}
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  func (g *CloudSQLGenerator) loadDBs(svc *sqladmin.Service, instanceName, project string) error {
    61  	DBs, err := svc.Databases.List(project, instanceName).Do()
    62  	if err != nil {
    63  		return err
    64  	}
    65  	for _, db := range DBs.Items {
    66  		g.Resources = append(g.Resources, terraformutils.NewResource(
    67  			instanceName+":"+db.Name,
    68  			instanceName+"-"+db.Name,
    69  			"google_sql_database",
    70  			g.ProviderName,
    71  			map[string]string{
    72  				"instance": instanceName,
    73  				"project":  project,
    74  				"name":     db.Name,
    75  			},
    76  
    77  			cloudSQLAllowEmptyValues,
    78  			cloudSQLAdditionalFields,
    79  		))
    80  	}
    81  	return nil
    82  }
    83  
    84  // Generate TerraformResources from GCP API,
    85  // from each databases create many TerraformResource(dbinstance + databases)
    86  // Need dbinstance name as ID for terraform resource
    87  func (g *CloudSQLGenerator) InitResources() error {
    88  	project := g.GetArgs()["project"].(string)
    89  	ctx := context.Background()
    90  	svc, err := sqladmin.NewService(ctx)
    91  	if err != nil {
    92  		return err
    93  	}
    94  	if err := g.loadDBInstances(svc, project); err != nil {
    95  		return err
    96  	}
    97  
    98  	return nil
    99  }