Skip to content

Commit bf90bc7

Browse files
committed
HCPCP-2315: POC support geography in provider
1 parent 8535513 commit bf90bc7

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

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.146.0
16+
github.com/hashicorp/hcp-sdk-go v0.151.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.146.0 h1:S6XnAvrcofTvYieEYNm2ATq1AgdYYTJDqBYAN22C/zY=
130-
github.com/hashicorp/hcp-sdk-go v0.146.0/go.mod h1:HYHwfLOi7gvZwtqTQfPRYlK+XDup9NWhVRDU6N24tOs=
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=
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,17 @@ import (
6767
radar_resource_service "github.com/hashicorp/hcp-sdk-go/clients/cloud-vault-radar/preview/2023-05-01/client/resource_service"
6868

6969
hcpConfig "github.com/hashicorp/hcp-sdk-go/config"
70+
hcpConfigGeography "github.com/hashicorp/hcp-sdk-go/config/geography"
7071
sdk "github.com/hashicorp/hcp-sdk-go/httpclient"
7172
)
7273

74+
const (
75+
// TODO: replace these two with the constants as defined in
76+
// hcp-sdk-go/config/geography
77+
GeographyUS = "us"
78+
GeographyEU = "eu"
79+
)
80+
7381
// Client is an HCP client capable of making requests on behalf of a service principal
7482
type Client struct {
7583
Config ClientConfig
@@ -104,6 +112,7 @@ type ClientConfig struct {
104112
ClientID string
105113
ClientSecret string
106114
CredentialFile string
115+
Geography string
107116

108117
// WorkloadIdentityTokenFile and WorkloadIdentityResourceName can be set to
109118
// indicate that authentication should occur by using workload identity
@@ -141,6 +150,17 @@ func NewClient(config ClientConfig) (*Client, error) {
141150
opts = append(opts, hcpConfig.WithCredentialFile(cf))
142151
}
143152

153+
// TODO: once the naming in hcp-sdk-go has been finalized, the following can
154+
// be changed to
155+
// if config.Geography != "" {
156+
// opts = append(opts, hcpConfig.WithGeography(config.Geography))
157+
// }
158+
if config.Geography == GeographyUS {
159+
opts = append(opts, hcpConfig.WithGeography(string(hcpConfigGeography.US)))
160+
} else if config.Geography == GeographyEU {
161+
opts = append(opts, hcpConfig.WithGeography(string(hcpConfigGeography.EU)))
162+
}
163+
144164
// Create the HCP Config
145165
hcp, err := hcpConfig.NewHCPConfig(opts...)
146166
if err != nil {

internal/provider/provider.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
"github.com/hashicorp/terraform-provider-hcp/internal/provider/waypoint"
3232
"github.com/hashicorp/terraform-provider-hcp/internal/provider/webhook"
3333
"github.com/hashicorp/terraform-provider-hcp/internal/statuspage"
34+
35+
"github.com/hashicorp/terraform-plugin-log/tflog"
3436
)
3537

3638
// This is an implementation using the Provider framework
@@ -48,6 +50,7 @@ type ProviderFrameworkModel struct {
4850
ProjectID types.String `tfsdk:"project_id"`
4951
WorkloadIdentity types.List `tfsdk:"workload_identity"`
5052
SkipStatusCheck types.Bool `tfsdk:"skip_status_check"`
53+
Geography types.String `tfsdk:"geography"`
5154
}
5255

5356
type WorkloadIdentityFrameworkModel struct {
@@ -62,6 +65,7 @@ func (p *ProviderFramework) Metadata(ctx context.Context, req provider.MetadataR
6265
}
6366

6467
func (p *ProviderFramework) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
68+
tflog.Info(ctx, "Configuring hcp schema")
6569
resp.Schema = schema.Schema{
6670
Attributes: map[string]schema.Attribute{
6771
"client_id": schema.StringAttribute{
@@ -98,6 +102,13 @@ func (p *ProviderFramework) Schema(ctx context.Context, req provider.SchemaReque
98102
Optional: true,
99103
Description: "When set to true, the provider will skip checking the HCP status page for service outages or returning warnings.",
100104
},
105+
"geography": schema.StringAttribute{
106+
Optional: true,
107+
Description: "The geography in which resources should be created.",
108+
Validators: []validator.String{
109+
stringvalidator.OneOf(clients.GeographyUS, clients.GeographyEU),
110+
},
111+
},
101112
},
102113
Blocks: map[string]schema.Block{
103114
// TODO migrate to SingleNestedAttribute once the providersdkv2 is
@@ -235,6 +246,8 @@ func NewFrameworkProvider(version string) func() provider.Provider {
235246
}
236247

237248
func (p *ProviderFramework) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
249+
tflog.Info(ctx, "Configuring hcp provider")
250+
238251
// Sets up HCP SDK client.
239252
var data ProviderFrameworkModel
240253
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
@@ -271,6 +284,14 @@ func (p *ProviderFramework) Configure(ctx context.Context, req provider.Configur
271284
}
272285
}
273286

287+
if data.Geography.IsNull() {
288+
tflog.Info(ctx, "Geography is null; using default")
289+
} else {
290+
clientConfig.Geography = data.Geography.ValueString()
291+
}
292+
tflog.Info(ctx, "Configure creating a new client", map[string]interface{}{
293+
"geography": clientConfig.Geography,
294+
})
274295
client, err := clients.NewClient(clientConfig)
275296
if err != nil {
276297
resp.Diagnostics.AddError(fmt.Sprintf("unable to create HCP api client: %v", err), "")

internal/providersdkv2/provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ func New() func() *schema.Provider {
8484
"Using a credential file allows you to authenticate the provider as a service principal via client " +
8585
"credentials or dynamically based on Workload Identity Federation.",
8686
},
87+
"geography": {
88+
Type: schema.TypeString,
89+
Optional: true,
90+
// Required: true,
91+
Description: "The geography in which resources should be created.",
92+
},
8793
"workload_identity": {
8894
Type: schema.TypeList,
8995
Optional: true,
@@ -147,6 +153,7 @@ func configure(p *schema.Provider) func(context.Context, *schema.ResourceData) (
147153
ClientSecret: d.Get("client_secret").(string),
148154
CredentialFile: d.Get("credential_file").(string),
149155
ProjectID: d.Get("project_id").(string),
156+
Geography: d.Get("geography").(string),
150157
SourceChannel: p.UserAgent("terraform-provider-hcp", version.ProviderVersion),
151158
}
152159

0 commit comments

Comments
 (0)