Skip to content

Commit ba022b4

Browse files
committed
1.add domain for tag client used for lock file. 2.adjust endpoint formation.
1 parent 806cb7e commit ba022b4

File tree

2 files changed

+35
-4
lines changed
  • internal/backend/remote-state/cos
  • website/docs/language/settings/backends

2 files changed

+35
-4
lines changed

internal/backend/remote-state/cos/backend.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ package cos
66
import (
77
"context"
88
"fmt"
9+
"log"
910
"net/http"
1011
"net/url"
1112
"os"
13+
"regexp"
1214
"strconv"
1315
"strings"
1416
"time"
@@ -29,6 +31,7 @@ const (
2931
PROVIDER_SECURITY_TOKEN = "TENCENTCLOUD_SECURITY_TOKEN"
3032
PROVIDER_REGION = "TENCENTCLOUD_REGION"
3133
PROVIDER_ENDPOINT = "TENCENTCLOUD_ENDPOINT"
34+
PROVIDER_DOMAIN = "TENCENTCLOUD_DOMAIN"
3235
PROVIDER_ASSUME_ROLE_ARN = "TENCENTCLOUD_ASSUME_ROLE_ARN"
3336
PROVIDER_ASSUME_ROLE_SESSION_NAME = "TENCENTCLOUD_ASSUME_ROLE_SESSION_NAME"
3437
PROVIDER_ASSUME_ROLE_SESSION_DURATION = "TENCENTCLOUD_ASSUME_ROLE_SESSION_DURATION"
@@ -50,6 +53,7 @@ type Backend struct {
5053
key string
5154
encrypt bool
5255
acl string
56+
domain string
5357
}
5458

5559
// New creates a new backend for TencentCloud cos remote state.
@@ -91,9 +95,15 @@ func New() backend.Backend {
9195
"endpoint": {
9296
Type: schema.TypeString,
9397
Optional: true,
94-
Description: "The custom endpoint for the COS API. eg: http://{Bucket}.cos-internal.{Region}.tencentcos.cn",
98+
Description: "The custom endpoint for the COS API. eg: http://cos-internal.{Region}.tencentcos.cn, both http or https can be accepted.",
9599
DefaultFunc: schema.EnvDefaultFunc(PROVIDER_ENDPOINT, nil),
96100
},
101+
"domain": {
102+
Type: schema.TypeString,
103+
Optional: true,
104+
DefaultFunc: schema.EnvDefaultFunc(PROVIDER_DOMAIN, nil),
105+
Description: "The root domain of the API request, Default is `tencentcloudapi.com`.",
106+
},
97107
"prefix": {
98108
Type: schema.TypeString,
99109
Optional: true,
@@ -238,10 +248,28 @@ func (b *Backend) configure(ctx context.Context) error {
238248
return err
239249
}
240250

251+
if v, ok := data.GetOk("domain"); ok {
252+
b.domain = v.(string)
253+
log.Printf("[DEBUG][backend] set domain for TencentCloud api client. domain:[%s]", b.domain)
254+
}
241255
// set url as endpoint when it provided
256+
// "http://{Bucket}.cos-internal.{Region}.tencentcos.cn"
242257
if v, ok := data.GetOk("endpoint"); ok {
243258
endpoint := v.(string)
244-
u, err = url.Parse(endpoint)
259+
260+
re := regexp.MustCompile(`^(http(s)?)://cos-internal\.([^.]+)\.tencentcos\.cn$`)
261+
matches := re.FindStringSubmatch(endpoint)
262+
if len(matches) != 4 {
263+
return fmt.Errorf("[backend] invalid URL, must be: %v", "http(s)://cos-internal.{Region}.tencentcos.cn")
264+
}
265+
266+
protocol := matches[1]
267+
region := matches[3]
268+
269+
// URL after converting
270+
newUrl := fmt.Sprintf("%s://%s.cos-internal.%s.tencentcos.cn", protocol, b.bucket, region)
271+
u, err = url.Parse(newUrl)
272+
log.Printf("[DEBUG][backend] set cos url as:[%s]", newUrl)
245273
}
246274
if err != nil {
247275
return err
@@ -349,6 +377,8 @@ func (b *Backend) NewClientProfile(timeout int) *profile.ClientProfile {
349377
cpf.HttpProfile.ReqMethod = "POST"
350378
// request timeout
351379
cpf.HttpProfile.ReqTimeout = timeout
380+
// request domain
381+
cpf.HttpProfile.RootDomain = b.domain
352382

353383
return cpf
354384
}

website/docs/language/settings/backends/cos.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ The following configuration options or environment variables are supported:
6565
- `acl` - (Optional) Object ACL to be applied to the state file, allows `private` and `public-read`. Defaults to `private`.
6666
- `accelerate` - (Optional) Whether to enable global Acceleration. Defaults to `false`.
6767
- `endpoint` - (Optional) The Custom Endpoint for the COS backend. It supports environment variables `TENCENTCLOUD_ENDPOINT`.
68+
- `domain` - (Optional) The root domain of the API request. Defaults to `tencentcloudapi.com`. It supports environment variables `TENCENTCLOUD_DOMAIN`.
6869

6970
### Assume Role
7071
If provided with an assume role, Terraform will attempt to assume this role using the supplied credentials.
@@ -112,7 +113,7 @@ $ terraform plan
112113
### Endpoint
113114
If provided with an endpoint url, Terraform will attempt to access COS backend by the `endpoint` configuration or environment variables `TENCENTCLOUD_ENDPOINT`.
114115

115-
A typical endpoint like this: `http://{Bucket}.cos-internal.{Region}.tencentcos.cn`, both http or https can be accepted.
116+
A typical endpoint like this: `http://cos-internal.{Region}.tencentcos.cn`, both http or https can be accepted.
116117

117118
Usage:
118119

@@ -122,7 +123,7 @@ terraform {
122123
region = "ap-guangzhou"
123124
bucket = "bucket-for-terraform-state-1258798060"
124125
prefix = "terraform/state"
125-
endpoint = "http://bucket-for-terraform-state-1258798060.cos-internal.ap-guangzhou.tencentcos.cn"
126+
endpoint = "http://cos-internal.ap-guangzhou.tencentcos.cn"
126127
}
127128
}
128129
```

0 commit comments

Comments
 (0)