Skip to content

Commit

Permalink
SCALRCORE-26654 Provider > Migrate scalr_tag resource and datasource (#…
Browse files Browse the repository at this point in the history
…365)

* SCALRCORE-26653 Introduce internal packages

* SCALRCORE-26653 Implement provider server muxing

* SCALRCORE-26653 Resolve conflicts

* SCALRCORE-26653 CI: bump Go to 1.23

* SCALRCORE-26653 CI: bump golangci-lint

* SCALRCORE-26653 Remove deprecated rand.Seed calls

* SCALRCORE-26653 Doc formatting

* SCALRCORE-26653 Fix goveralls CI step

* SCALRCORE-26654 Move existing provider modules to internal/provider package

* SCALRCORE-26654 Move existing provider modules to internal/provider package

* SCALRCORE-26654 Inject version in SDK provider

* SCALRCORE-26654 Fix refactoring typos

* SCALRCORE-26654 Get rid of terraform-plugin-sdk logging package in http client

* SCALRCORE-26654 Rename datasource files

* SCALRCORE-26654 Add tag datasource; replace SDK imports in test modules with framework substitutes

* SCALRCORE-26654 * wip

* SCALRCORE-26654 * wip

* SCALRCORE-26654 * wip

* SCALRCORE-26654 * wip - preserve file move history

* SCALRCORE-26654 Replace legacy pointer fns with generic ptr()

* SCALRCORE-26654 Add tag resource

* SCALRCORE-26654 Add tag resource

* SCALRCORE-26654 Fix linter

* SCALRCORE-26654 Implement skaffolding generator

* SCALRCORE-26654 Add skaff readme

* SCALRCORE-26654 Add sdk->framework upgrade tests
  • Loading branch information
petroprotsakh authored Dec 12, 2024
1 parent 92e388f commit 002158c
Show file tree
Hide file tree
Showing 141 changed files with 2,448 additions and 1,847 deletions.
10 changes: 9 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,12 @@ test-compile:
fi
go test -c $(TEST) $(TESTARGS)

.PHONY: build test testacc vet fmt test-compile notify-upstream
resource:
@echo "Skaffolding resource $(name)..."
@cd skaff && go run cmd/main.go -type=resource -name=$(name)

datasource:
@echo "Skaffolding datasource $(name)..."
@cd skaff && go run cmd/main.go -type=data_source -name=$(name)

.PHONY: build test testacc vet fmt test-compile notify-upstream resource datasource
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.13.0
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0
github.com/hashicorp/terraform-plugin-go v0.25.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-mux v0.17.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0
github.com/hashicorp/terraform-plugin-testing v1.11.0
github.com/hashicorp/terraform-svchost v0.1.1
github.com/scalr/go-scalr v0.0.0-20241111151759-2c33e05ca45e
)
Expand Down Expand Up @@ -40,7 +42,7 @@ require (
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/hc-install v0.9.0 // indirect
github.com/hashicorp/hcl/v2 v2.22.0 // indirect
github.com/hashicorp/hcl/v2 v2.23.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.21.0 // indirect
github.com/hashicorp/terraform-json v0.23.0 // indirect
Expand All @@ -66,14 +68,14 @@ require (
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.15.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/mod v0.21.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20200904004341-0bd0a958aa1d // indirect
Expand Down
28 changes: 16 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ github.com/hashicorp/hc-install v0.9.0 h1:2dIk8LcvANwtv3QZLckxcjyF5w8KVtiMxu6G6e
github.com/hashicorp/hc-install v0.9.0/go.mod h1:+6vOP+mf3tuGgMApVYtmsnDoKWMDcFXeTxCACYZ8SFg=
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce h1:xdsDDbiBDQTKASoGEZ+pEmF1OnWuu8AQ9I8iNbHNeno=
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=
github.com/hashicorp/hcl/v2 v2.22.0 h1:hkZ3nCtqeJsDhPRFz5EA9iwcG1hNWGePOTw6oyul12M=
github.com/hashicorp/hcl/v2 v2.22.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/hashicorp/hcl/v2 v2.23.0 h1:Fphj1/gCylPxHutVSEOf2fBOh1VE4AuLV7+kbJf3qos=
github.com/hashicorp/hcl/v2 v2.23.0/go.mod h1:62ZYHrXgPoX8xBnzl8QzbWq4dyDsDtfCRgIq1rbJEvA=
github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI65Y=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
github.com/hashicorp/terraform-exec v0.21.0 h1:uNkLAe95ey5Uux6KJdua6+cv8asgILFVWkd/RG0D2XQ=
Expand All @@ -124,6 +124,8 @@ github.com/hashicorp/terraform-plugin-docs v0.16.0 h1:UmxFr3AScl6Wged84jndJIfFcc
github.com/hashicorp/terraform-plugin-docs v0.16.0/go.mod h1:M3ZrlKBJAbPMtNOPwHicGi1c+hZUh7/g0ifT/z7TVfA=
github.com/hashicorp/terraform-plugin-framework v1.13.0 h1:8OTG4+oZUfKgnfTdPTJwZ532Bh2BobF4H+yBiYJ/scw=
github.com/hashicorp/terraform-plugin-framework v1.13.0/go.mod h1:j64rwMGpgM3NYXTKuxrCnyubQb/4VKldEKlcG8cvmjU=
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0 h1:RXMmu7JgpFjnI1a5QjMCBb11usrW2OtAG+iOTIj5c9Y=
github.com/hashicorp/terraform-plugin-framework-validators v0.15.0/go.mod h1:Bh89/hNmqsEWug4/XWKYBwtnw3tbz5BAy1L1OgvbIaY=
github.com/hashicorp/terraform-plugin-go v0.25.0 h1:oi13cx7xXA6QciMcpcFi/rwA974rdTxjqEhXJjbAyks=
github.com/hashicorp/terraform-plugin-go v0.25.0/go.mod h1:+SYagMYadJP86Kvn+TGeV+ofr/R3g4/If0O5sO96MVw=
github.com/hashicorp/terraform-plugin-log v0.9.0 h1:i7hOA+vdAItN1/7UrfBqBwvYPQ9TFvymaRGZED3FCV0=
Expand All @@ -132,6 +134,8 @@ github.com/hashicorp/terraform-plugin-mux v0.17.0 h1:/J3vv3Ps2ISkbLPiZOLspFcIZ0v
github.com/hashicorp/terraform-plugin-mux v0.17.0/go.mod h1:yWuM9U1Jg8DryNfvCp+lH70WcYv6D8aooQxxxIzFDsE=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0 h1:wyKCCtn6pBBL46c1uIIBNUOWlNfYXfXpVo16iDyLp8Y=
github.com/hashicorp/terraform-plugin-sdk/v2 v2.35.0/go.mod h1:B0Al8NyYVr8Mp/KLwssKXG1RqnTk7FySqSn4fRuLNgw=
github.com/hashicorp/terraform-plugin-testing v1.11.0 h1:MeDT5W3YHbONJt2aPQyaBsgQeAIckwPX41EUHXEn29A=
github.com/hashicorp/terraform-plugin-testing v1.11.0/go.mod h1:WNAHQ3DcgV/0J+B15WTE6hDvxcUdkPPpnB1FR3M910U=
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
Expand Down Expand Up @@ -238,8 +242,8 @@ golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
Expand Down Expand Up @@ -267,8 +271,8 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -284,21 +288,21 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
Expand Down
12 changes: 7 additions & 5 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ import (
"net/http"
"net/url"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
svchost "github.com/hashicorp/terraform-svchost"
"github.com/hashicorp/terraform-svchost"
"github.com/hashicorp/terraform-svchost/disco"
"github.com/scalr/go-scalr"

"github.com/scalr/terraform-provider-scalr/internal/logging"
)

var scalrServiceIDs = []string{"iacp.v3"}

// Configure configures and returns a new Scalr client.
func Configure(h, t, v string) (*scalr.Client, error) {
// Parse the hostname for comparison,
// Parse the hostname for comparison
hostname, err := svchost.ForComparison(h)
if err != nil {
return nil, err
Expand All @@ -31,7 +33,7 @@ func Configure(h, t, v string) (*scalr.Client, error) {
credsSrc := credentialsSource(config)
services := disco.NewWithCredentialsSource(credsSrc)
services.SetUserAgent(providerUaString)
services.Transport = logging.NewLoggingHTTPTransport(services.Transport)
services.Transport = logging.NewLoggingTransport(services.Transport)

// Add any static host configurations service discovery object.
for userHost, hostConfig := range config.Hosts {
Expand Down Expand Up @@ -93,7 +95,7 @@ func Configure(h, t, v string) (*scalr.Client, error) {
}

httpClient := scalr.DefaultConfig().HTTPClient
httpClient.Transport = logging.NewLoggingHTTPTransport(httpClient.Transport)
httpClient.Transport = logging.NewLoggingTransport(httpClient.Transport)

headers := make(http.Header)
headers.Add("User-Agent", providerUaString)
Expand Down
12 changes: 6 additions & 6 deletions internal/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"

"github.com/hashicorp/hcl"
svchost "github.com/hashicorp/terraform-svchost"
"github.com/hashicorp/terraform-svchost"
"github.com/hashicorp/terraform-svchost/auth"
)

Expand All @@ -17,15 +17,15 @@ const (

// config is the structure of the configuration for the Terraform CLI.
type config struct {
Hosts map[string]*configHost `hcl:"host"`
Credentials map[string]map[string]interface{} `hcl:"credentials"`
Hosts map[string]*configHost `hcl:"host"`
Credentials map[string]map[string]any `hcl:"credentials"`
}

// configHost is the structure of the "host" nested block within the CLI
// configuration, which can be used to override the default service host
// discovery behavior for a particular hostname.
type configHost struct {
Services map[string]interface{} `hcl:"services"`
Services map[string]any `hcl:"services"`
}

// CliConfig tries to find and parse the configuration of the Terraform CLI.
Expand Down Expand Up @@ -57,7 +57,7 @@ func cliConfig() *config {
// hostnames.
combinedConfig.Credentials = credentialsConfig.Credentials
if combinedConfig.Credentials == nil {
combinedConfig.Credentials = make(map[string]map[string]interface{})
combinedConfig.Credentials = make(map[string]map[string]any)
}
for host, creds := range mainConfig.Credentials {
combinedConfig.Credentials[host] = creds
Expand All @@ -71,7 +71,7 @@ func credentialsSource(config *config) auth.CredentialsSource {

// Add all configured credentials to the credentials source.
if len(config.Credentials) > 0 {
staticTable := map[svchost.Hostname]map[string]interface{}{}
staticTable := map[svchost.Hostname]map[string]any{}
for userHost, creds := range config.Credentials {
host, err := svchost.ForComparison(userHost)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions internal/framework/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package framework

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"

"github.com/scalr/go-scalr"
)

type DataSourceWithScalrClient struct {
Client *scalr.Client
}

func (d *DataSourceWithScalrClient) Configure(_ context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

c, ok := req.ProviderData.(*scalr.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *scalr.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

d.Client = c
}
54 changes: 54 additions & 0 deletions internal/framework/defaults/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package defaults

import (
"context"
"fmt"
"os"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/types"
)

const CurrentAccountIDEnvVar = "SCALR_ACCOUNT_ID"

func GetDefaultScalrAccountID() (string, diag.Diagnostics) {
var diags diag.Diagnostics
v := os.Getenv(CurrentAccountIDEnvVar)
if v == "" {
diags.AddError(
"Cannot infer current account",
fmt.Sprintf(
"Default value for `account_id` could not be computed."+
"\nIf you are using Scalr Provider for local runs, please set the attribute in resources explicitly,"+
"\nor export `%s` environment variable prior the run.",
CurrentAccountIDEnvVar,
),
)
}
return v, diags
}

// AccountIDRequired returns a default account id value handler.
//
// Use AccountIDRequired when a default value for account id must be set.
func AccountIDRequired() defaults.String {
return accountIDRequiredDefault{}
}

// accountIDRequiredDefault implements defaults.String
type accountIDRequiredDefault struct{}

func (r accountIDRequiredDefault) Description(_ context.Context) string {
return "value defaults to current Scalr account id"
}

func (r accountIDRequiredDefault) MarkdownDescription(ctx context.Context) string {
return r.Description(ctx)
}

func (r accountIDRequiredDefault) DefaultString(_ context.Context, _ defaults.StringRequest, resp *defaults.StringResponse) {
s, diags := GetDefaultScalrAccountID()
resp.Diagnostics.Append(diags...)
resp.PlanValue = types.StringValue(s)
}
32 changes: 32 additions & 0 deletions internal/framework/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package framework

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource"

"github.com/scalr/go-scalr"
)

type ResourceWithScalrClient struct {
Client *scalr.Client
}

func (r *ResourceWithScalrClient) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

c, ok := req.ProviderData.(*scalr.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *scalr.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)

return
}

r.Client = c
}
45 changes: 45 additions & 0 deletions internal/framework/validation/string_not_whitespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package validation

import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)

// Compile-time interface check
var _ validator.String = stringIsNotWhiteSpaceValidator{}

type stringIsNotWhiteSpaceValidator struct{}

func (v stringIsNotWhiteSpaceValidator) Description(_ context.Context) string {
return "must not be empty or consisting entirely of whitespace characters"
}

func (v stringIsNotWhiteSpaceValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}

func (v stringIsNotWhiteSpaceValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
return
}

value := req.ConfigValue.ValueString()

if strings.TrimSpace(value) == "" {
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
req.Path,
v.Description(ctx),
fmt.Sprintf("%q", value),
))

return
}
}

func StringIsNotWhiteSpace() validator.String {
return stringIsNotWhiteSpaceValidator{}
}
Loading

0 comments on commit 002158c

Please sign in to comment.