github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gitlab/group.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 gitlab
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"strconv"
    22  	"strings"
    23  
    24  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    25  	"github.com/xanzy/go-gitlab"
    26  )
    27  
    28  type GroupGenerator struct {
    29  	GitLabService
    30  }
    31  
    32  // Generate TerraformResources from gitlab API,
    33  func (g *GroupGenerator) InitResources() error {
    34  	ctx := context.Background()
    35  	client, err := g.createClient()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	group := g.Args["group"].(string)
    41  	g.Resources = append(g.Resources, createGroups(ctx, client, group)...)
    42  
    43  	return nil
    44  }
    45  
    46  func createGroups(ctx context.Context, client *gitlab.Client, groupID string) []terraformutils.Resource {
    47  	resources := []terraformutils.Resource{}
    48  	group, _, err := client.Groups.GetGroup(groupID, gitlab.WithContext(ctx))
    49  	if err != nil {
    50  		log.Println(err)
    51  		return nil
    52  	}
    53  
    54  	resource := terraformutils.NewSimpleResource(
    55  		strconv.FormatInt(int64(group.ID), 10),
    56  		getGroupResourceName(group),
    57  		"gitlab_group",
    58  		"gitlab",
    59  		[]string{},
    60  	)
    61  
    62  	// NOTE: mirror fields from API doesn't match with the ones from terraform provider
    63  	resource.IgnoreKeys = []string{"mirror_trigger_builds", "only_mirror_protected_branches", "mirror", "mirror_overwrites_diverged_branches"}
    64  
    65  	resource.SlowQueryRequired = true
    66  	resources = append(resources, resource)
    67  	resources = append(resources, createGroupVariables(ctx, client, group)...)
    68  	resources = append(resources, createGroupMembership(ctx, client, group)...)
    69  
    70  	return resources
    71  }
    72  func createGroupVariables(ctx context.Context, client *gitlab.Client, group *gitlab.Group) []terraformutils.Resource {
    73  	resources := []terraformutils.Resource{}
    74  	opt := &gitlab.ListGroupVariablesOptions{}
    75  
    76  	for {
    77  		groupVariables, resp, err := client.GroupVariables.ListVariables(group.ID, opt, gitlab.WithContext(ctx))
    78  		if err != nil {
    79  			log.Println(err)
    80  			return nil
    81  		}
    82  
    83  		for _, groupVariable := range groupVariables {
    84  
    85  			resource := terraformutils.NewSimpleResource(
    86  				fmt.Sprintf("%d:%s:%s", group.ID, groupVariable.Key, groupVariable.EnvironmentScope),
    87  				fmt.Sprintf("%s___%s___%s", getGroupResourceName(group), groupVariable.Key, groupVariable.EnvironmentScope),
    88  				"gitlab_group_variable",
    89  				"gitlab",
    90  				[]string{},
    91  			)
    92  			resource.SlowQueryRequired = true
    93  			resources = append(resources, resource)
    94  		}
    95  
    96  		if resp.NextPage == 0 {
    97  			break
    98  		}
    99  		opt.Page = resp.NextPage
   100  	}
   101  	return resources
   102  }
   103  
   104  func createGroupMembership(ctx context.Context, client *gitlab.Client, group *gitlab.Group) []terraformutils.Resource {
   105  	resources := []terraformutils.Resource{}
   106  	opt := &gitlab.ListGroupMembersOptions{}
   107  
   108  	for {
   109  		groupMembers, resp, err := client.Groups.ListGroupMembers(group.ID, opt, gitlab.WithContext(ctx))
   110  		if err != nil {
   111  			log.Println(err)
   112  			return nil
   113  		}
   114  
   115  		for _, groupMember := range groupMembers {
   116  
   117  			resource := terraformutils.NewSimpleResource(
   118  				fmt.Sprintf("%d:%d", group.ID, groupMember.ID),
   119  				fmt.Sprintf("%s___%s", getGroupResourceName(group), groupMember.Username),
   120  				"gitlab_group_membership",
   121  				"gitlab",
   122  				[]string{},
   123  			)
   124  			resource.SlowQueryRequired = true
   125  			resources = append(resources, resource)
   126  		}
   127  
   128  		if resp.NextPage == 0 {
   129  			break
   130  		}
   131  		opt.Page = resp.NextPage
   132  	}
   133  	return resources
   134  }
   135  
   136  func getGroupResourceName(group *gitlab.Group) string {
   137  	return fmt.Sprintf("%d___%s", group.ID, strings.ReplaceAll(group.FullPath, "/", "__"))
   138  }