github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/cloudflare/cloudflare_service.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 cloudflare
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"os"
    21  
    22  	"github.com/GoogleCloudPlatform/terraformer/terraformutils"
    23  	cf "github.com/cloudflare/cloudflare-go"
    24  )
    25  
    26  type CloudflareService struct { //nolint
    27  	terraformutils.Service
    28  }
    29  
    30  func (s *CloudflareService) initializeAPI() (*cf.API, error) {
    31  	apiKey := os.Getenv("CLOUDFLARE_API_KEY")
    32  	apiEmail := os.Getenv("CLOUDFLARE_EMAIL")
    33  	apiToken := os.Getenv("CLOUDFLARE_API_TOKEN")
    34  	accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
    35  
    36  	if apiToken == "" && (apiEmail == "" || apiKey == "") {
    37  		err := errors.New("Either CLOUDFLARE_API_TOKEN or CLOUDFLARE_API_KEY/CLOUDFLARE_EMAIL environment variables must be set")
    38  		fmt.Fprintln(os.Stderr, err)
    39  		return nil, err
    40  	}
    41  
    42  	if apiToken != "" {
    43  		return cf.NewWithAPIToken(apiToken, cf.UsingAccount(accountID))
    44  	}
    45  
    46  	return cf.New(apiKey, apiEmail, cf.UsingAccount(accountID))
    47  }