Skip to content

Commit fb32914

Browse files
authored
HCPCP-2321 add geography provider schema (#1354)
* HCPCP-2321: add geography provider to provider schema * Set the default geography in config explicitly
1 parent 59b50b0 commit fb32914

File tree

8 files changed

+40
-3
lines changed

8 files changed

+40
-3
lines changed

.changelog/1354.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:feature
2+
Add geography to provider configuration.
3+
```

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ resource "hcp_vault_cluster" "example" {
125125
- `client_id` (String) The OAuth2 Client ID for API operations.
126126
- `client_secret` (String) The OAuth2 Client Secret for API operations.
127127
- `credential_file` (String) The path to an HCP credential file to use to authenticate the provider to HCP. You can alternatively set the HCP_CRED_FILE environment variable to point at a credential file as well. Using a credential file allows you to authenticate the provider as a service principal via client credentials or dynamically based on Workload Identity Federation.
128+
- `geography` (String) The geography in which HCP resources should be created. Default is `us`.
128129
- `project_id` (String) The default project in which resources should be created.
129130
- `skip_status_check` (Boolean) When set to true, the provider will skip checking the HCP status page for service outages or returning warnings.
130131
- `workload_identity` (Block List) Allows authenticating the provider by exchanging the OAuth 2.0 access token or OpenID Connect token specified in the `token_file` for a HCP service principal using Workload Identity Federation. (see [below for nested schema](#nestedblock--workload_identity))

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/hashicorp/go-cty v1.5.0
1414
github.com/hashicorp/go-uuid v1.0.3
1515
github.com/hashicorp/go-version v1.7.0
16-
github.com/hashicorp/hcp-sdk-go v0.151.0
16+
github.com/hashicorp/hcp-sdk-go v0.154.0
1717
github.com/hashicorp/terraform-plugin-docs v0.20.1
1818
github.com/hashicorp/terraform-plugin-framework v1.15.0
1919
github.com/hashicorp/terraform-plugin-framework-validators v0.18.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ github.com/hashicorp/hc-install v0.9.2 h1:v80EtNX4fCVHqzL9Lg/2xkp62bbvQMnvPQ0G+O
126126
github.com/hashicorp/hc-install v0.9.2/go.mod h1:XUqBQNnuT4RsxoxiM9ZaUk0NX8hi2h+Lb6/c0OZnC/I=
127127
github.com/hashicorp/hcl/v2 v2.23.0 h1:Fphj1/gCylPxHutVSEOf2fBOh1VE4AuLV7+kbJf3qos=
128128
github.com/hashicorp/hcl/v2 v2.23.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
129-
github.com/hashicorp/hcp-sdk-go v0.151.0 h1:dPLgab1oihguGcOqjFoI8y+iZ05dwGK9euEvG5tzyXY=
130-
github.com/hashicorp/hcp-sdk-go v0.151.0/go.mod h1:HYHwfLOi7gvZwtqTQfPRYlK+XDup9NWhVRDU6N24tOs=
129+
github.com/hashicorp/hcp-sdk-go v0.154.0 h1:o8Sytq4cWF8kJXAUpN+bZbonKKUR8ZRS1yv3y5SCrGM=
130+
github.com/hashicorp/hcp-sdk-go v0.154.0/go.mod h1:HYHwfLOi7gvZwtqTQfPRYlK+XDup9NWhVRDU6N24tOs=
131131
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
132132
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
133133
github.com/hashicorp/terraform-exec v0.23.0 h1:MUiBM1s0CNlRFsCLJuM5wXZrzA3MnPYEsiXmzATMW/I=

internal/clients/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/hashicorp/hcp-sdk-go/auth"
1313
"github.com/hashicorp/hcp-sdk-go/auth/workload"
14+
"github.com/hashicorp/hcp-sdk-go/config/geography"
1415
"github.com/hashicorp/terraform-plugin-framework/types"
1516
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1617

@@ -127,6 +128,9 @@ type ClientConfig struct {
127128
// SourceChannel denotes the client (channel) that originated the HCP cluster request.
128129
// this is synonymous to a user-agent.
129130
SourceChannel string
131+
132+
// Geography denotes the geography the HCP client should operate in.
133+
Geography string
130134
}
131135

132136
// NewClient creates a new Client that is capable of making HCP requests
@@ -141,6 +145,14 @@ func NewClient(config ClientConfig) (*Client, error) {
141145
opts = append(opts, hcpConfig.WithCredentialFile(cf))
142146
}
143147

148+
if config.Geography == "" {
149+
// If geography is not set, default to the one used by the SDK.
150+
// Currently default is us.
151+
config.Geography = string(geography.Default)
152+
} else {
153+
opts = append(opts, hcpConfig.WithGeography(config.Geography))
154+
}
155+
144156
// Create the HCP Config
145157
hcp, err := hcpConfig.NewHCPConfig(opts...)
146158
if err != nil {

internal/clients/client_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func Test_loadCredentialFile(t *testing.T) {
1919
"empty config": {
2020
config: ClientConfig{},
2121
},
22+
"load with geography": {
23+
config: ClientConfig{
24+
Geography: "eu",
25+
},
26+
},
2227
"ignores with only resource": {
2328
config: ClientConfig{
2429
WorkloadIdentityResourceName: "my_resource",

internal/provider/provider.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
2222
"github.com/hashicorp/terraform-plugin-framework/types"
2323

24+
"github.com/hashicorp/hcp-sdk-go/config/geography"
2425
"github.com/hashicorp/terraform-provider-hcp/internal/clients"
2526
"github.com/hashicorp/terraform-provider-hcp/internal/provider/iam"
2627
"github.com/hashicorp/terraform-provider-hcp/internal/provider/logstreaming"
@@ -48,6 +49,7 @@ type ProviderFrameworkModel struct {
4849
ProjectID types.String `tfsdk:"project_id"`
4950
WorkloadIdentity types.List `tfsdk:"workload_identity"`
5051
SkipStatusCheck types.Bool `tfsdk:"skip_status_check"`
52+
Geography types.String `tfsdk:"geography"`
5153
}
5254

5355
type WorkloadIdentityFrameworkModel struct {
@@ -98,6 +100,13 @@ func (p *ProviderFramework) Schema(ctx context.Context, req provider.SchemaReque
98100
Optional: true,
99101
Description: "When set to true, the provider will skip checking the HCP status page for service outages or returning warnings.",
100102
},
103+
"geography": schema.StringAttribute{
104+
Optional: true,
105+
Description: "The geography in which HCP resources should be created. Default is `us`.",
106+
Validators: []validator.String{
107+
stringvalidator.OneOf(string(geography.US), string(geography.EU)),
108+
},
109+
},
101110
},
102111
Blocks: map[string]schema.Block{
103112
// TODO migrate to SingleNestedAttribute once the providersdkv2 is
@@ -253,6 +262,7 @@ func (p *ProviderFramework) Configure(ctx context.Context, req provider.Configur
253262
CredentialFile: data.CredentialFile.ValueString(),
254263
ProjectID: data.ProjectID.ValueString(),
255264
SourceChannel: "terraform-provider-hcp",
265+
Geography: data.Geography.ValueString(),
256266
}
257267

258268
// Read the workload_identity configuration.

internal/providersdkv2/provider.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ func New() func() *schema.Provider {
115115
Default: false,
116116
Description: "When set to true, the provider will skip checking the HCP status page for service outages or returning warnings.",
117117
},
118+
"geography": {
119+
Type: schema.TypeString,
120+
Optional: true,
121+
Description: "The geography in which HCP resources should be created. Default is `us`.",
122+
},
118123
},
119124
ProviderMetaSchema: map[string]*schema.Schema{
120125
"module_name": {
@@ -147,6 +152,7 @@ func configure(p *schema.Provider) func(context.Context, *schema.ResourceData) (
147152
ClientSecret: d.Get("client_secret").(string),
148153
CredentialFile: d.Get("credential_file").(string),
149154
ProjectID: d.Get("project_id").(string),
155+
Geography: d.Get("geography").(string),
150156
SourceChannel: p.UserAgent("terraform-provider-hcp", version.ProviderVersion),
151157
}
152158

0 commit comments

Comments
 (0)