github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gcp/logging.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  	"google.golang.org/api/iterator"
    22  
    23  	"cloud.google.com/go/logging/logadmin"
    24  )
    25  
    26  var loggingAllowEmptyValues = []string{}
    27  
    28  var loggingAdditionalFields = map[string]interface{}{}
    29  
    30  type LoggingGenerator struct {
    31  	GCPService
    32  }
    33  
    34  func (g *LoggingGenerator) loadLoggingMetrics(ctx context.Context, client *logadmin.Client) error {
    35  	metricIterator := client.Metrics(ctx)
    36  
    37  	for {
    38  		metric, err := metricIterator.Next()
    39  
    40  		if err == iterator.Done {
    41  			break
    42  		}
    43  		if err != nil {
    44  			return err
    45  		}
    46  		g.Resources = append(g.Resources, terraformutils.NewResource(
    47  			metric.ID,
    48  			metric.ID,
    49  			"google_logging_metric",
    50  			g.ProviderName,
    51  			map[string]string{
    52  				"name":    metric.ID,
    53  				"project": g.GetArgs()["project"].(string),
    54  			},
    55  			loggingAllowEmptyValues,
    56  			loggingAdditionalFields,
    57  		))
    58  	}
    59  	return nil
    60  }
    61  
    62  // Generate TerraformResources from GCP API
    63  func (g *LoggingGenerator) InitResources() error {
    64  	project := g.GetArgs()["project"].(string)
    65  	ctx := context.Background()
    66  	client, err := logadmin.NewClient(ctx, project)
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	if err := g.loadLoggingMetrics(ctx, client); err != nil {
    72  		return err
    73  	}
    74  
    75  	return nil
    76  }