github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/yandex/yandex_provider.go (about)

     1  // Copyright 2019 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 yandex
    16  
    17  import (
    18  	"errors"
    19  	"os"
    20  
    21  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    22  )
    23  
    24  type YandexProvider struct { //nolint
    25  	terraformutils.Provider
    26  	oauthToken string
    27  	folderID   string
    28  }
    29  
    30  func (p *YandexProvider) Init(args []string) error {
    31  	if os.Getenv("YC_TOKEN") == "" {
    32  		return errors.New("set YC_TOKEN env var")
    33  	}
    34  	p.oauthToken = os.Getenv("YC_TOKEN")
    35  
    36  	if len(args) > 0 {
    37  		//  first args is target folder ID
    38  		p.folderID = args[0]
    39  	} else {
    40  		if os.Getenv("YC_FOLDER_ID") == "" {
    41  			return errors.New("set YC_FOLDER_ID env var")
    42  		}
    43  		p.folderID = os.Getenv("YC_FOLDER_ID")
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func (p *YandexProvider) GetName() string {
    50  	return "yandex"
    51  }
    52  
    53  func (p *YandexProvider) GetProviderData(arg ...string) map[string]interface{} {
    54  	return map[string]interface{}{}
    55  }
    56  
    57  func (YandexProvider) GetResourceConnections() map[string]map[string][]string {
    58  	return map[string]map[string][]string{}
    59  }
    60  
    61  func (p *YandexProvider) GetSupportedService() map[string]terraformutils.ServiceGenerator {
    62  	return map[string]terraformutils.ServiceGenerator{
    63  		"disk":     &DiskGenerator{},
    64  		"instance": &InstanceGenerator{},
    65  		"network":  &NetworkGenerator{},
    66  		"subnet":   &SubnetGenerator{},
    67  	}
    68  }
    69  
    70  func (p *YandexProvider) InitService(serviceName string, verbose bool) error {
    71  	var isSupported bool
    72  	if _, isSupported = p.GetSupportedService()[serviceName]; !isSupported {
    73  		return errors.New("yandex: " + serviceName + " not supported service")
    74  	}
    75  	p.Service = p.GetSupportedService()[serviceName]
    76  	p.Service.SetName(serviceName)
    77  	p.Service.SetVerbose(verbose)
    78  	p.Service.SetProviderName(p.GetName())
    79  	p.Service.SetArgs(map[string]interface{}{
    80  		"folder_id": p.folderID,
    81  		"token":     p.oauthToken,
    82  	})
    83  	return nil
    84  }