|
| 1 | +package oss |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/aliyun/aliyun-oss-go-sdk/oss" |
| 7 | + "github.com/denverdino/aliyungo/common" |
| 8 | + "github.com/denverdino/aliyungo/location" |
| 9 | + "github.com/hashicorp/terraform/backend" |
| 10 | + "github.com/hashicorp/terraform/helper/schema" |
| 11 | + "github.com/hashicorp/terraform/terraform" |
| 12 | + "os" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "log" |
| 16 | +) |
| 17 | + |
| 18 | +// New creates a new backend for OSS remote state. |
| 19 | +func New() backend.Backend { |
| 20 | + s := &schema.Backend{ |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "bucket": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + Description: "The name of the OSS bucket", |
| 26 | + }, |
| 27 | + |
| 28 | + "key": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "The path to the state file inside the bucket", |
| 32 | + ValidateFunc: func(v interface{}, s string) ([]string, []error) { |
| 33 | + // oss will strip leading slashes from an object, so while this will |
| 34 | + // technically be accepted by oss, it will break our workspace hierarchy. |
| 35 | + if strings.HasPrefix(v.(string), "/") { |
| 36 | + return nil, []error{fmt.Errorf("key must not start with '/'")} |
| 37 | + } |
| 38 | + return nil, nil |
| 39 | + }, |
| 40 | + }, |
| 41 | + "access_key": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + Description: "Alibaba Cloud Access Key ID", |
| 45 | + DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_ACCESS_KEY", ""), |
| 46 | + }, |
| 47 | + |
| 48 | + "secret_key": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Description: "Alibaba Cloud Access Secret Key", |
| 52 | + DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_SECRET_KEY", ""), |
| 53 | + }, |
| 54 | + |
| 55 | + "security_token": { |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + Description: "Alibaba Cloud Security Token", |
| 59 | + DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_SECURITY_TOKEN", os.Getenv("SECURITY_TOKEN")), |
| 60 | + }, |
| 61 | + |
| 62 | + "region": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + Description: "The region of the OSS bucket. It will be ignored when 'endpoint' is specified.", |
| 66 | + DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_REGION", "cn-beijing"), |
| 67 | + }, |
| 68 | + |
| 69 | + "endpoint": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Optional: true, |
| 72 | + Description: "A custom endpoint for the OSS API", |
| 73 | + DefaultFunc: schema.EnvDefaultFunc("ALICLOUD_OSS_ENDPOINT", ""), |
| 74 | + }, |
| 75 | + |
| 76 | + "encrypt": { |
| 77 | + Type: schema.TypeBool, |
| 78 | + Optional: true, |
| 79 | + Description: "Whether to enable server side encryption of the state file", |
| 80 | + Default: false, |
| 81 | + }, |
| 82 | + |
| 83 | + "acl": { |
| 84 | + Type: schema.TypeString, |
| 85 | + Optional: true, |
| 86 | + Description: "Object ACL to be applied to the state file", |
| 87 | + Default: "", |
| 88 | + }, |
| 89 | + |
| 90 | + "workspace_key_prefix": { |
| 91 | + Type: schema.TypeString, |
| 92 | + Optional: true, |
| 93 | + Description: "The prefix applied to the non-default state path inside the bucket", |
| 94 | + Default: "workspaces", |
| 95 | + }, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + result := &Backend{Backend: s} |
| 100 | + result.Backend.ConfigureFunc = result.configure |
| 101 | + return result |
| 102 | +} |
| 103 | + |
| 104 | +type Backend struct { |
| 105 | + *schema.Backend |
| 106 | + |
| 107 | + // The fields below are set from configure |
| 108 | + ossClient *oss.Client |
| 109 | + |
| 110 | + bucketName string |
| 111 | + keyName string |
| 112 | + serverSideEncryption bool |
| 113 | + acl string |
| 114 | + security_token string |
| 115 | + endpoint string |
| 116 | + workspaceKeyPrefix string |
| 117 | +} |
| 118 | + |
| 119 | +func (b *Backend) configure(ctx context.Context) error { |
| 120 | + if b.ossClient != nil { |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + // Grab the resource data |
| 125 | + data := schema.FromContextBackendConfig(ctx) |
| 126 | + |
| 127 | + b.bucketName = data.Get("bucket").(string) |
| 128 | + b.keyName = data.Get("key").(string) |
| 129 | + b.serverSideEncryption = data.Get("encrypt").(bool) |
| 130 | + b.acl = data.Get("acl").(string) |
| 131 | + b.workspaceKeyPrefix = data.Get("workspace_key_prefix").(string) |
| 132 | + access_key := data.Get("access_key").(string) |
| 133 | + secret_key := data.Get("secret_key").(string) |
| 134 | + security_token := data.Get("security_token").(string) |
| 135 | + endpoint := data.Get("endpoint").(string) |
| 136 | + if endpoint == "" { |
| 137 | + region := common.Region(data.Get("region").(string)) |
| 138 | + if end, err := b.getOSSEndpointByRegion(access_key, secret_key, region); err != nil { |
| 139 | + return err |
| 140 | + } else { |
| 141 | + endpoint = end |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + log.Printf("[DEBUG] Instantiate OSS client using endpoint: %#v", endpoint) |
| 146 | + var options []oss.ClientOption |
| 147 | + if security_token != "" { |
| 148 | + options = append(options, oss.SecurityToken(security_token)) |
| 149 | + } |
| 150 | + options = append(options, oss.UserAgent(fmt.Sprintf("HashiCorp-Terraform-v%s", terraform.VersionString()))) |
| 151 | + |
| 152 | + if client, err := oss.New(fmt.Sprintf("http://%s", endpoint), access_key, secret_key, options...); err != nil { |
| 153 | + return err |
| 154 | + } else { |
| 155 | + b.ossClient = client |
| 156 | + } |
| 157 | + |
| 158 | + return nil |
| 159 | +} |
| 160 | + |
| 161 | +func (b *Backend) getOSSEndpointByRegion(access_key, secret_key string, region common.Region) (string, error) { |
| 162 | + |
| 163 | + endpoints, err := location.NewClient(access_key, secret_key).DescribeEndpoints(&location.DescribeEndpointsArgs{ |
| 164 | + Id: region, |
| 165 | + ServiceCode: "oss", |
| 166 | + Type: "openAPI", |
| 167 | + }) |
| 168 | + if err != nil { |
| 169 | + return "", fmt.Errorf("Describe endpoint using region: %#v got an error: %#v.", region, err) |
| 170 | + } |
| 171 | + endpointItem := endpoints.Endpoints.Endpoint |
| 172 | + endpoint := "" |
| 173 | + if endpointItem != nil && len(endpointItem) > 0 { |
| 174 | + endpoint = endpointItem[0].Endpoint |
| 175 | + } |
| 176 | + |
| 177 | + return endpoint, nil |
| 178 | +} |
0 commit comments