Skip to content

Commit 6aeaa0d

Browse files
wip
1 parent 277bdbf commit 6aeaa0d

File tree

11 files changed

+329
-2
lines changed

11 files changed

+329
-2
lines changed

linode/acceptance/provider_factories.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/hashicorp/terraform-plugin-framework/provider"
78
"github.com/hashicorp/terraform-plugin-framework/providerserver"
89
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
910
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
@@ -46,3 +47,29 @@ var HttpExternalProviders = map[string]resource.ExternalProvider{
4647
Source: "hashicorp/http",
4748
},
4849
}
50+
51+
var ProtoV6CustomProviderFactories = map[string]func(provider provider.Provider) (tfprotov6.ProviderServer, error){
52+
"linode": func(provider provider.Provider) (tfprotov6.ProviderServer, error) {
53+
ctx := context.Background()
54+
55+
upgradedSDKProvider, err := tf5to6server.UpgradeServer(
56+
ctx,
57+
TestAccSDKv2Providers["linode"].GRPCProvider,
58+
)
59+
if err != nil {
60+
return nil, fmt.Errorf("failed to upgrade SDKv2 GRPC provider: %w", err)
61+
}
62+
63+
providers := []func() tfprotov6.ProviderServer{
64+
providerserver.NewProtocol6(provider),
65+
func() tfprotov6.ProviderServer { return upgradedSDKProvider },
66+
}
67+
68+
muxServer, err := tf6muxserver.NewMuxServer(ctx, providers...)
69+
if err != nil {
70+
return nil, err
71+
}
72+
73+
return muxServer.ProviderServer(), nil
74+
},
75+
}

linode/acceptance/util.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,3 +762,28 @@ func GetTestClient() (*linodego.Client, error) {
762762

763763
return client, nil
764764
}
765+
766+
func GetTestClientAlternateToken(tokenName string) (*linodego.Client, error) {
767+
token := os.Getenv(tokenName)
768+
if token == "" {
769+
return nil, fmt.Errorf("%s must be set for acceptance tests", tokenName)
770+
}
771+
772+
apiVersion := os.Getenv("LINODE_API_VERSION")
773+
if apiVersion == "" {
774+
apiVersion = "v4beta"
775+
}
776+
777+
config := &helper.Config{
778+
AccessToken: token,
779+
APIVersion: apiVersion,
780+
APIURL: os.Getenv("LINODE_URL"),
781+
}
782+
783+
client, err := config.Client(context.Background())
784+
if err != nil {
785+
return nil, err
786+
}
787+
788+
return client, nil
789+
}

linode/acceptance/with_client.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package acceptance
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/provider"
7+
"github.com/linode/linodego"
8+
"github.com/linode/terraform-provider-linode/v3/linode"
9+
"github.com/linode/terraform-provider-linode/v3/linode/helper"
10+
)
11+
12+
type FrameworkProviderWithClient struct {
13+
linode.FrameworkProvider
14+
client *linodego.Client
15+
}
16+
17+
func NewFrameworkProviderWithClient(
18+
client *linodego.Client,
19+
) provider.Provider {
20+
return &FrameworkProviderWithClient{
21+
FrameworkProvider: *TestAccFrameworkProvider,
22+
client: client,
23+
}
24+
}
25+
26+
func (fp *FrameworkProviderWithClient) Configure(
27+
ctx context.Context,
28+
req provider.ConfigureRequest,
29+
resp *provider.ConfigureResponse,
30+
) {
31+
// Call parent configure func
32+
fp.FrameworkProvider.Configure(ctx, req, resp)
33+
34+
resp.ResourceData.(*helper.FrameworkProviderMeta).Client = fp.client
35+
resp.DataSourceData.(*helper.FrameworkProviderMeta).Client = fp.client
36+
}

linode/consumerimagesharegrouptoken/framework_resource.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package consumerimagesharegrouptoken
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/hashicorp/terraform-plugin-framework/resource"
78
"github.com/hashicorp/terraform-plugin-framework/types"
89
"github.com/hashicorp/terraform-plugin-log/tflog"
@@ -130,7 +131,7 @@ func (r *Resource) Update(
130131
token, err := client.ImageShareGroupUpdateToken(ctx, tokenUUID, updateOpts)
131132
if err != nil {
132133
resp.Diagnostics.AddError(
133-
fmt.Sprintf("Failed to update Image Share Group Token (%d).", tokenUUID),
134+
fmt.Sprintf("Failed to update Image Share Group Token (%s).", tokenUUID),
134135
err.Error(),
135136
)
136137
return
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//go:build integration || consumerimagesharegrouptoken
2+
3+
package consumerimagesharegrouptoken_test
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/linode/terraform-provider-linode/v3/linode/acceptance"
13+
"github.com/linode/terraform-provider-linode/v3/linode/consumerimagesharegrouptoken/tmpl"
14+
)
15+
16+
// This test requires two separate Linode API tokens, one for the producer
17+
// and one for the consumer.
18+
//
19+
// These can be set using the LINODE_PRODUCER_TOKEN and LINODE_CONSUMER_TOKEN
20+
// environment variables.
21+
//
22+
// If either is not set,the test will be skipped.
23+
func TestAccResourceImageShareGroupToken_basic(t *testing.T) {
24+
t.Parallel()
25+
26+
if os.Getenv("LINODE_PRODUCER_TOKEN") == "" || os.Getenv("LINODE_CONSUMER_TOKEN") == "" {
27+
t.Skip("Skipping test: both LINODE_PRODUCER_TOKEN and LINODE_CONSUMER_TOKEN must be set")
28+
}
29+
30+
// Define provider factories for both producer and consumer
31+
factories := map[string]func() (tfprotov6.ProviderServer, error){
32+
"linode-producer": acceptance.NewTokenProviderFactory("LINODE_PRODUCER_TOKEN"),
33+
"linode-consumer": acceptance.NewTokenProviderFactory("LINODE_CONSUMER_TOKEN"),
34+
}
35+
36+
resourceName := "linode_consumer_image_share_group_token.foobar"
37+
shareGroupLabel := acctest.RandomWithPrefix("tf-test")
38+
tokenLabel := acctest.RandomWithPrefix("tf-test")
39+
tokenLabelUpdated := tokenLabel + "-updated"
40+
41+
resource.Test(t, resource.TestCase{
42+
PreCheck: func() { acceptance.PreCheck(t) },
43+
ProtoV6ProviderFactories: factories,
44+
Steps: []resource.TestStep{
45+
{
46+
Config: tmpl.Basic(t, shareGroupLabel, tokenLabel),
47+
Check: resource.ComposeTestCheckFunc(
48+
resource.TestCheckResourceAttrSet(resourceName, "token"),
49+
resource.TestCheckResourceAttrSet(resourceName, "token_uuid"),
50+
resource.TestCheckResourceAttrSet(resourceName, "status"),
51+
resource.TestCheckResourceAttr(resourceName, "label", tokenLabel),
52+
resource.TestCheckResourceAttrSet(resourceName, "valid_for_sharegroup_uuid"),
53+
resource.TestCheckNoResourceAttr(resourceName, "sharegroup_uuid"),
54+
resource.TestCheckNoResourceAttr(resourceName, "sharegroup_label"),
55+
),
56+
},
57+
{
58+
Config: tmpl.Basic(t, shareGroupLabel, tokenLabelUpdated),
59+
Check: resource.ComposeTestCheckFunc(
60+
resource.TestCheckResourceAttrSet(resourceName, "token"),
61+
resource.TestCheckResourceAttrSet(resourceName, "token_uuid"),
62+
resource.TestCheckResourceAttrSet(resourceName, "status"),
63+
resource.TestCheckResourceAttr(resourceName, "label", tokenLabelUpdated),
64+
resource.TestCheckResourceAttrSet(resourceName, "valid_for_sharegroup_uuid"),
65+
resource.TestCheckNoResourceAttr(resourceName, "sharegroup_uuid"),
66+
resource.TestCheckNoResourceAttr(resourceName, "sharegroup_label"),
67+
),
68+
},
69+
},
70+
})
71+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{ define "consumer_image_share_group_token_basic" }}
2+
3+
resource "linode_producer_image_share_group" "foobar" {
4+
provider = linode-producer
5+
label = "{{ .ShareGroupLabel }}"
6+
description = "Example description"
7+
}
8+
9+
resource "linode_consumer_image_share_group_token" "foobar" {
10+
provider = linode-consumer
11+
depends_on = [linode_producer_image_share_group.foobar]
12+
valid_for_sharegroup_uuid = linode_producer_image_share_group.foobar.uuid
13+
label = "{{ .TokenLabel }}"
14+
}
15+
16+
{{ end }}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package tmpl
2+
3+
import (
4+
"testing"
5+
6+
"github.com/linode/terraform-provider-linode/v3/linode/acceptance"
7+
)
8+
9+
type TemplateData struct {
10+
ShareGroupLabel string
11+
TokenLabel string
12+
}
13+
14+
func Basic(t testing.TB, shareGroupLabel, tokenLabel string) string {
15+
return acceptance.ExecuteTemplate(t,
16+
"consumer_image_share_group_token_basic", TemplateData{
17+
ShareGroupLabel: shareGroupLabel,
18+
TokenLabel: tokenLabel,
19+
})
20+
}

linode/framework_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package linode
22

33
import (
44
"context"
5-
"github.com/linode/terraform-provider-linode/v3/linode/consumerimagesharegrouptoken"
65

76
"github.com/hashicorp/terraform-plugin-framework/datasource"
87
"github.com/hashicorp/terraform-plugin-framework/provider"
@@ -17,6 +16,7 @@ import (
1716
"github.com/linode/terraform-provider-linode/v3/linode/backup"
1817
"github.com/linode/terraform-provider-linode/v3/linode/childaccount"
1918
"github.com/linode/terraform-provider-linode/v3/linode/childaccounts"
19+
"github.com/linode/terraform-provider-linode/v3/linode/consumerimagesharegrouptoken"
2020
"github.com/linode/terraform-provider-linode/v3/linode/databasebackups"
2121
"github.com/linode/terraform-provider-linode/v3/linode/databaseengines"
2222
"github.com/linode/terraform-provider-linode/v3/linode/databasemysql"
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//go:build integration || producerimagesharegroupmember
2+
3+
package producerimagesharegroupmember_test
4+
5+
import (
6+
"os"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
10+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
12+
"github.com/linode/terraform-provider-linode/v3/linode/acceptance"
13+
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroupmember/tmpl"
14+
)
15+
16+
// This test requires two separate Linode API tokens, one for the producer
17+
// and one for the consumer.
18+
//
19+
// These can be set using the LINODE_PRODUCER_TOKEN and LINODE_CONSUMER_TOKEN
20+
// environment variables.
21+
//
22+
// If either is not set,the test will fail.
23+
func TestAccResourceImageShareGroupMember_basic(t *testing.T) {
24+
t.Parallel()
25+
26+
producerToken := os.Getenv("LINODE_PRODUCER_TOKEN")
27+
consumerToken := os.Getenv("LINODE_CONSUMER_TOKEN")
28+
29+
if producerToken == "" || consumerToken == "" {
30+
t.Skip("Skipping test: both LINODE_PRODUCER_TOKEN and LINODE_CONSUMER_TOKEN must be set")
31+
}
32+
33+
producerClient, err := acceptance.GetTestClientAlternateToken("LINODE_PRODUCER_TOKEN")
34+
if err != nil {
35+
t.Fatalf("Failed to create producer client: %s", err)
36+
}
37+
38+
consumerClient, err := acceptance.GetTestClientAlternateToken("LINODE_CONSUMER_TOKEN")
39+
if err != nil {
40+
t.Fatalf("Failed to create consumer client: %s", err)
41+
}
42+
43+
producerProvider := acceptance.NewFrameworkProviderWithClient(producerClient)
44+
consumerProvider := acceptance.NewFrameworkProviderWithClient(consumerClient)
45+
46+
resourceName := "linode_producer_image_share_group_member.foobar"
47+
shareGroupLabel := acctest.RandomWithPrefix("tf-test")
48+
tokenLabel := acctest.RandomWithPrefix("tf-test")
49+
memberLabel := acctest.RandomWithPrefix("tf-test")
50+
memberLabelUpdated := memberLabel + "-updated"
51+
52+
resource.Test(t, resource.TestCase{
53+
PreCheck: func() { acceptance.PreCheck(t) },
54+
ProtoV6ProviderFactories: map[string]func() (tfprotov6.ProviderServer, error){
55+
"linode-producer": func() (tfprotov6.ProviderServer, error) {
56+
return acceptance.ProtoV6CustomProviderFactories["linode"](producerProvider)
57+
},
58+
"linode-consumer": func() (tfprotov6.ProviderServer, error) {
59+
return acceptance.ProtoV6CustomProviderFactories["linode"](consumerProvider)
60+
},
61+
},
62+
Steps: []resource.TestStep{
63+
{
64+
Config: tmpl.Basic(t, shareGroupLabel, tokenLabel, memberLabel),
65+
Check: resource.ComposeTestCheckFunc(
66+
resource.TestCheckResourceAttrSet(resourceName, "sharegroup_id"),
67+
resource.TestCheckResourceAttrSet(resourceName, "token"),
68+
resource.TestCheckResourceAttrSet(resourceName, "token_uuid"),
69+
resource.TestCheckResourceAttrSet(resourceName, "status"),
70+
resource.TestCheckResourceAttr(resourceName, "label", memberLabel),
71+
),
72+
},
73+
{
74+
Config: tmpl.Basic(t, shareGroupLabel, tokenLabel, memberLabelUpdated),
75+
Check: resource.ComposeTestCheckFunc(
76+
resource.TestCheckResourceAttrSet(resourceName, "sharegroup_id"),
77+
resource.TestCheckResourceAttrSet(resourceName, "token"),
78+
resource.TestCheckResourceAttrSet(resourceName, "token_uuid"),
79+
resource.TestCheckResourceAttrSet(resourceName, "status"),
80+
resource.TestCheckResourceAttr(resourceName, "label", memberLabelUpdated),
81+
),
82+
},
83+
},
84+
})
85+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{{ define "producer_image_share_group_member_basic" }}
2+
3+
resource "linode_producer_image_share_group" "foobar" {
4+
provider = linode-producer
5+
label = "{{ .ShareGroupLabel }}"
6+
description = "Example description"
7+
}
8+
9+
resource "linode_consumer_image_share_group_token" "foobar" {
10+
provider = linode-consumer
11+
depends_on = [linode_producer_image_share_group.foobar]
12+
valid_for_sharegroup_uuid = linode_producer_image_share_group.foobar.uuid
13+
label = "{{ .TokenLabel }}"
14+
}
15+
16+
resource "linode_producer_image_share_group_member" "foobar" {
17+
provider = linode-producer
18+
depends_on = [linode_producer_image_share_group.foobar, linode_consumer_image_share_group_token.foobar]
19+
sharegroup_id = linode_producer_image_share_group.foobar.id
20+
token = linode_consumer_image_share_group_token.foobar.token
21+
label = "{{ .MemberLabel }}"
22+
}
23+
24+
{{ end }}

0 commit comments

Comments
 (0)