github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/gitlab/gitlab_provider.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 gitlab
    16  
    17  import (
    18  	"os"
    19  
    20  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    21  	"github.com/pkg/errors"
    22  	"github.com/zclconf/go-cty/cty"
    23  )
    24  
    25  type GitLabProvider struct { //nolint
    26  	terraformutils.Provider
    27  	group   string
    28  	token   string
    29  	baseURL string
    30  }
    31  
    32  func (p GitLabProvider) GetResourceConnections() map[string]map[string][]string {
    33  	return map[string]map[string][]string{}
    34  }
    35  
    36  func (p GitLabProvider) GetProviderData(arg ...string) map[string]interface{} {
    37  	return map[string]interface{}{
    38  		"provider": map[string]interface{}{
    39  			"gitlab": map[string]interface{}{
    40  				// TODO: Should I add some default config here?
    41  				// "token": p.token,
    42  				// "base_url": p.baseURL,
    43  			},
    44  		},
    45  	}
    46  }
    47  
    48  func (p *GitLabProvider) GetConfig() cty.Value {
    49  	return cty.ObjectVal(map[string]cty.Value{
    50  		"token": cty.StringVal(p.token),
    51  		// NOTE: Real provider doesn't support empty/null base_url, only set when there's value
    52  		"base_url": cty.StringVal(p.baseURL),
    53  	})
    54  }
    55  
    56  // Init GitLabProvider with group
    57  func (p *GitLabProvider) Init(args []string) error {
    58  	p.group = args[0]
    59  	p.baseURL = gitLabDefaultURL
    60  	if len(args) < 2 {
    61  		if os.Getenv("GITLAB_TOKEN") == "" {
    62  			return errors.New("token requirement")
    63  		}
    64  		p.token = os.Getenv("GITLAB_TOKEN")
    65  	} else {
    66  		p.token = args[1]
    67  	}
    68  	if len(args) > 2 {
    69  		if args[2] != "" {
    70  			p.baseURL = args[2]
    71  		}
    72  	}
    73  	return nil
    74  }
    75  
    76  func (p *GitLabProvider) GetName() string {
    77  	return "gitlab"
    78  }
    79  
    80  func (p *GitLabProvider) InitService(serviceName string, verbose bool) error {
    81  	var isSupported bool
    82  	if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
    83  		return errors.New(p.GetName() + ": " + serviceName + " not supported service")
    84  	}
    85  	p.Service = p.GetSupportedService()[serviceName]
    86  	p.Service.SetName(serviceName)
    87  	p.Service.SetVerbose(verbose)
    88  	p.Service.SetProviderName(p.GetName())
    89  	p.Service.SetArgs(map[string]interface{}{
    90  		"group":    p.group,
    91  		"token":    p.token,
    92  		"base_url": p.baseURL,
    93  	})
    94  	return nil
    95  }
    96  
    97  // GetSupportedService return map of support service for gitlab
    98  func (p *GitLabProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
    99  	return map[string]terraformutils.ServiceGenerator{
   100  		"projects": &ProjectGenerator{},
   101  		"groups":   &GroupGenerator{},
   102  	}
   103  }