mirror of https://github.com/hashicorp/packer
yandex-import: allow set custom API endpoint (#9850)
* Separate Access Config from yandex builder Config * make use of Access Config explicit * Move `MaxRetries` into AccessConfig * NewDriverYC use AccessConfig instead Config * yandex-import PP use common Access Config Now support set custom API Endpoint * yandex-export PP use common Access Config Now support set custom API Endpoint too (as yandex-import) * fix test * Tiny doc updates.pull/9855/head
parent
f578b93f7e
commit
804fefef17
@ -0,0 +1,67 @@
|
||||
//go:generate struct-markdown
|
||||
|
||||
package yandex
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/hashicorp/packer/packer"
|
||||
"github.com/hashicorp/packer/template/interpolate"
|
||||
"github.com/yandex-cloud/go-sdk/iamkey"
|
||||
)
|
||||
|
||||
const defaultEndpoint = "api.cloud.yandex.net:443"
|
||||
|
||||
// AccessConfig is for common configuration related to Yandex.Cloud API access
|
||||
type AccessConfig struct {
|
||||
// Non standard API endpoint. Default is `api.cloud.yandex.net:443`.
|
||||
Endpoint string `mapstructure:"endpoint" required:"false"`
|
||||
// Path to file with Service Account key in json format. This
|
||||
// is an alternative method to authenticate to Yandex.Cloud. Alternatively you may set environment variable
|
||||
// `YC_SERVICE_ACCOUNT_KEY_FILE`.
|
||||
ServiceAccountKeyFile string `mapstructure:"service_account_key_file" required:"false"`
|
||||
// OAuth token to use to authenticate to Yandex.Cloud. Alternatively you may set
|
||||
// value by environment variable `YC_TOKEN`.
|
||||
Token string `mapstructure:"token" required:"true"`
|
||||
// The maximum number of times an API request is being executed.
|
||||
MaxRetries int `mapstructure:"max_retries"`
|
||||
}
|
||||
|
||||
func (c *AccessConfig) Prepare(ctx *interpolate.Context) []error {
|
||||
var errs []error
|
||||
|
||||
if c.Endpoint == "" {
|
||||
c.Endpoint = defaultEndpoint
|
||||
}
|
||||
|
||||
// provision config by OS environment variables
|
||||
if c.Token == "" {
|
||||
c.Token = os.Getenv("YC_TOKEN")
|
||||
}
|
||||
|
||||
if c.ServiceAccountKeyFile == "" {
|
||||
c.ServiceAccountKeyFile = os.Getenv("YC_SERVICE_ACCOUNT_KEY_FILE")
|
||||
}
|
||||
|
||||
if c.Token != "" && c.ServiceAccountKeyFile != "" {
|
||||
errs = append(errs, errors.New("one of token or service account key file must be specified, not both"))
|
||||
}
|
||||
|
||||
if c.Token != "" {
|
||||
packer.LogSecretFilter.Set(c.Token)
|
||||
}
|
||||
|
||||
if c.ServiceAccountKeyFile != "" {
|
||||
if _, err := iamkey.ReadFromJSONFile(c.ServiceAccountKeyFile); err != nil {
|
||||
errs = append(errs, fmt.Errorf("fail to read service account key file: %s", err))
|
||||
}
|
||||
}
|
||||
|
||||
if len(errs) > 0 {
|
||||
return errs
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
<!-- Code generated from the comments of the AccessConfig struct in builder/yandex/access_config.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `endpoint` (string) - Non standard API endpoint. Default is `api.cloud.yandex.net:443`.
|
||||
|
||||
- `service_account_key_file` (string) - Path to file with Service Account key in json format. This
|
||||
is an alternative method to authenticate to Yandex.Cloud. Alternatively you may set environment variable
|
||||
`YC_SERVICE_ACCOUNT_KEY_FILE`.
|
||||
|
||||
- `max_retries` (int) - The maximum number of times an API request is being executed.
|
||||
@ -0,0 +1,4 @@
|
||||
<!-- Code generated from the comments of the AccessConfig struct in builder/yandex/access_config.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `token` (string) - OAuth token to use to authenticate to Yandex.Cloud. Alternatively you may set
|
||||
value by environment variable `YC_TOKEN`.
|
||||
@ -0,0 +1,3 @@
|
||||
<!-- Code generated from the comments of the AccessConfig struct in builder/yandex/access_config.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
AccessConfig is for common configuration related to Yandex.Cloud API access
|
||||
@ -1,13 +1,10 @@
|
||||
<!-- Code generated from the comments of the Config struct in builder/yandex/config.go; DO NOT EDIT MANUALLY -->
|
||||
|
||||
- `folder_id` (string) - The folder ID that will be used to launch instances and store images.
|
||||
Alternatively you may set value by environment variable YC_FOLDER_ID.
|
||||
Alternatively you may set value by environment variable `YC_FOLDER_ID`.
|
||||
To use a different folder for looking up the source image or saving the target image to
|
||||
check options 'source_image_folder_id' and 'target_image_folder_id'.
|
||||
|
||||
- `token` (string) - OAuth token to use to authenticate to Yandex.Cloud. Alternatively you may set
|
||||
value by environment variable YC_TOKEN.
|
||||
|
||||
- `source_image_family` (string) - The source image family to create the new image
|
||||
from. You can also specify source_image_id instead. Just one of a source_image_id or
|
||||
source_image_family must be specified. Example: `ubuntu-1804-lts`
|
||||
source_image_family must be specified. Example: `ubuntu-1804-lts`.
|
||||
|
||||
Loading…
Reference in new issue