github.com/NaverCloudPlatform/ncloud-sdk-go-v2@v1.6.13/ncloud/configuration.go (about)

     1  package ncloud
     2  
     3  import (
     4  	"bufio"
     5  	"github.com/NaverCloudPlatform/ncloud-sdk-go-v2/ncloud/credentials"
     6  	"log"
     7  	"net/http"
     8  	"os"
     9  	"os/user"
    10  	"path/filepath"
    11  	"strings"
    12  )
    13  
    14  type APIKey struct {
    15  	AccessKey string
    16  	SecretKey string
    17  }
    18  
    19  type Configuration struct {
    20  	BasePath      string            `json:"basePath,omitempty"`
    21  	Host          string            `json:"host,omitempty"`
    22  	Scheme        string            `json:"scheme,omitempty"`
    23  	DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
    24  	UserAgent     string            `json:"userAgent,omitempty"`
    25  	HTTPClient    *http.Client
    26  	APIKey        *APIKey
    27  	Credentials   *credentials.Credentials
    28  }
    29  
    30  // Keys This is for backward compatibility. Will be deprecated soon.
    31  func Keys() *APIKey {
    32  	apiKey := &APIKey{
    33  		AccessKey: "",
    34  		SecretKey: "",
    35  	}
    36  
    37  	usr, err := user.Current()
    38  	if err != nil {
    39  		log.Fatal(err)
    40  		return nil
    41  	}
    42  
    43  	if usr.HomeDir == "" {
    44  		log.Fatal("use.HomeDir is nil")
    45  		return nil
    46  	}
    47  
    48  	configureFile := filepath.Join(usr.HomeDir, ".ncloud", "configure")
    49  	file, err := os.Open(configureFile)
    50  	if err != nil {
    51  		log.Fatal(err)
    52  		return nil
    53  	}
    54  	defer file.Close()
    55  
    56  	scanner := bufio.NewScanner(file)
    57  	for scanner.Scan() {
    58  		line := scanner.Text()
    59  		s := strings.Split(line, "=")
    60  		switch strings.TrimSpace(s[0]) {
    61  		case "ncloud_access_key_id":
    62  			apiKey.AccessKey = strings.TrimSpace(s[1])
    63  		case "ncloud_secret_access_key":
    64  			apiKey.SecretKey = strings.TrimSpace(s[1])
    65  		}
    66  	}
    67  
    68  	if err := scanner.Err(); err != nil {
    69  		log.Fatal(err)
    70  		return nil
    71  	}
    72  
    73  	return apiKey
    74  }
    75  
    76  func (c *Configuration) AddDefaultHeader(key string, value string) {
    77  	c.DefaultHeader[key] = value
    78  }
    79  
    80  func (c *Configuration) GetCredentials() *credentials.Credentials {
    81  	if c.ValidCredentials() {
    82  		return c.Credentials.Retrieve()
    83  	}
    84  	return nil
    85  }
    86  
    87  func (c *Configuration) ValidCredentials() bool {
    88  	return c.Credentials != nil && c.Credentials.Valid()
    89  }
    90  
    91  func (c *Configuration) InitCredentials() {
    92  	if c.APIKey != nil {
    93  		c.Credentials = credentials.NewValueProviderCreds(c.APIKey.AccessKey, c.APIKey.SecretKey)
    94  	} else if !c.ValidCredentials() {
    95  		c.Credentials = credentials.LoadCredentials(credentials.DefaultCredentialsChain())
    96  	}
    97  }