github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gmailfilter/label.go (about)

     1  // Copyright 2020 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 gmailfilter
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  	"google.golang.org/api/gmail/v1"
    23  )
    24  
    25  type LabelGenerator struct {
    26  	GmailfilterService
    27  }
    28  
    29  func (g LabelGenerator) createResources(labels []*gmail.Label) []terraformutils.Resource {
    30  	var resources []terraformutils.Resource
    31  	for _, l := range labels {
    32  		if l.Type == "system" {
    33  			continue // ignore system labels
    34  		}
    35  		resources = append(resources, terraformutils.NewResource(
    36  			l.Id,
    37  			strings.ReplaceAll(l.Name, "/", "_"),
    38  			"gmailfilter_label",
    39  			"gmailfilter",
    40  			map[string]string{},
    41  			[]string{},
    42  			map[string]interface{}{}))
    43  	}
    44  	return resources
    45  }
    46  
    47  func (g *LabelGenerator) InitResources() error {
    48  	ctx := context.Background()
    49  	gmailService, err := g.gmailService(ctx)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	labels, err := gmailService.Users.Labels.List(gmailUser).Do()
    55  	if err != nil {
    56  		return err
    57  	}
    58  	g.Resources = append(g.Resources, g.createResources(labels.Labels)...)
    59  
    60  	return nil
    61  
    62  }