Skip to content

Commit cf7a7d1

Browse files
authored
feat: implement stackit git instances (#791)
* feat: implement stackit git instances * review changes
1 parent c7c64a5 commit cf7a7d1

File tree

16 files changed

+937
-96
lines changed

16 files changed

+937
-96
lines changed

docs/data-sources/git.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_git Data Source - stackit"
4+
subcategory: ""
5+
description: |-
6+
Git Instance datasource schema.
7+
~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources.
8+
---
9+
10+
# stackit_git (Data Source)
11+
12+
Git Instance datasource schema.
13+
14+
~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources.
15+
16+
## Example Usage
17+
18+
```terraform
19+
data "stackit_git" "git" {
20+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21+
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
22+
}
23+
```
24+
25+
<!-- schema generated by tfplugindocs -->
26+
## Schema
27+
28+
### Required
29+
30+
- `instance_id` (String) ID linked to the git instance.
31+
- `project_id` (String) STACKIT project ID to which the git instance is associated.
32+
33+
### Read-Only
34+
35+
- `id` (String) Terraform's internal resource ID, structured as "`project_id`,`instance_id`".
36+
- `name` (String) Unique name linked to the git instance.
37+
- `url` (String) Url linked to the git instance.
38+
- `version` (String) Version linked to the git instance.

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Note: AWS specific checks must be skipped as they do not work on STACKIT. For de
159159
- `dns_custom_endpoint` (String) Custom endpoint for the DNS service
160160
- `enable_beta_resources` (Boolean) Enable beta resources. Default is false.
161161
- `experiments` (List of String) Enables experiments. These are unstable features without official support. More information can be found in the README. Available Experiments: [iam]
162+
- `git_custom_endpoint` (String) Custom endpoint for the Git service
162163
- `iaas_custom_endpoint` (String) Custom endpoint for the IaaS service
163164
- `loadbalancer_custom_endpoint` (String) Custom endpoint for the Load Balancer service
164165
- `logme_custom_endpoint` (String) Custom endpoint for the LogMe service

docs/resources/git.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "stackit_git Resource - stackit"
4+
subcategory: ""
5+
description: |-
6+
Git Instance resource schema.
7+
~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our guide https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources for how to opt-in to use beta resources.
8+
---
9+
10+
# stackit_git (Resource)
11+
12+
Git Instance resource schema.
13+
14+
~> This resource is in beta and may be subject to breaking changes in the future. Use with caution. See our [guide](https://registry.terraform.io/providers/stackitcloud/stackit/latest/docs/guides/opting_into_beta_resources) for how to opt-in to use beta resources.
15+
16+
## Example Usage
17+
18+
```terraform
19+
resource "stackit_git" "git" {
20+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
21+
name = "git-example-instance"
22+
}
23+
```
24+
25+
<!-- schema generated by tfplugindocs -->
26+
## Schema
27+
28+
### Required
29+
30+
- `name` (String) Unique name linked to the git instance.
31+
- `project_id` (String) STACKIT project ID to which the git instance is associated.
32+
33+
### Read-Only
34+
35+
- `id` (String) Terraform's internal resource ID, structured as "`project_id`,`instance_id`".
36+
- `instance_id` (String) ID linked to the git instance.
37+
- `url` (String) Url linked to the git instance.
38+
- `version` (String) Version linked to the git instance.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data "stackit_git" "git" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
instance_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource "stackit_git" "git" {
2+
project_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
3+
name = "git-example-instance"
4+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require (
1414
github.com/stackitcloud/stackit-sdk-go/core v0.17.1
1515
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.0.1
1616
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.2
17+
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.2
1718
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.1
1819
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.0.2
1920
github.com/stackitcloud/stackit-sdk-go/services/logme v0.22.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ github.com/stackitcloud/stackit-sdk-go/services/cdn v1.0.1 h1:fWCWCefwgPTJ0BIbFQ
158158
github.com/stackitcloud/stackit-sdk-go/services/cdn v1.0.1/go.mod h1:Gd+M/UZR0rIaHRXhJzkb0r7kl/nDRmpcmqnOPN5fCSQ=
159159
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.2 h1:6rb3EM0yXuMIBd1U6WsJoMzEiVaHC3WQFWFvT23OE4Y=
160160
github.com/stackitcloud/stackit-sdk-go/services/dns v0.13.2/go.mod h1:PMHoavoIaRZpkI9BA0nsnRjGoHASVSBon45XB3QyhMA=
161+
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.2 h1:AguA1s9FuwLmSyvGNv4I/O9u7c/x7cMwyCsrM6Tm2SU=
162+
github.com/stackitcloud/stackit-sdk-go/services/git v0.3.2/go.mod h1:XhXHJpOVC9Rpwyf1G+EpMbprBafH9aZb8vWBdR+z0WM=
161163
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.1 h1:JXcLcbVesTtwVVb+jJjU3o0FmSpXBRnOw6PVETaeK+E=
162164
github.com/stackitcloud/stackit-sdk-go/services/iaas v0.22.1/go.mod h1:QNH50Pq0Hu21lLDOwa02PIjRjTl0LfEdHoz5snGQRn8=
163165
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.0.2 h1:5rVt3n7kDJvJQxFCtxfx8uZI9PGkvJY9fVJ4yar10Uc=

stackit/internal/core/core.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type ProviderData struct {
2323
AuthorizationCustomEndpoint string
2424
CdnCustomEndpoint string
2525
DnsCustomEndpoint string
26+
GitCustomEndpoint string
2627
IaaSCustomEndpoint string
2728
LoadBalancerCustomEndpoint string
2829
LogMeCustomEndpoint string
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package git
2+
3+
import (
4+
"context"
5+
_ "embed"
6+
"fmt"
7+
"maps"
8+
"strings"
9+
"testing"
10+
11+
"github.com/hashicorp/terraform-plugin-testing/config"
12+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
13+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
14+
"github.com/hashicorp/terraform-plugin-testing/terraform"
15+
stackitSdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
16+
"github.com/stackitcloud/stackit-sdk-go/core/utils"
17+
"github.com/stackitcloud/stackit-sdk-go/services/git"
18+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
19+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/testutil"
20+
)
21+
22+
//go:embed testdata/resource.tf
23+
var resourceConfig string
24+
25+
var name = fmt.Sprintf("git-%s-instance", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
26+
var nameUpdated = fmt.Sprintf("git-%s-instance", acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum))
27+
28+
var testConfigVars = config.Variables{
29+
"project_id": config.StringVariable(testutil.ProjectId),
30+
"name": config.StringVariable(name),
31+
}
32+
33+
func testConfigVarsUpdated() config.Variables {
34+
tempConfig := make(config.Variables, len(testConfigVars))
35+
maps.Copy(tempConfig, testConfigVars)
36+
// update git instance to a new name
37+
// should trigger creating a new instance
38+
tempConfig["name"] = config.StringVariable(nameUpdated)
39+
return tempConfig
40+
}
41+
42+
func TestGitInstance(t *testing.T) {
43+
resource.Test(t, resource.TestCase{
44+
ProtoV6ProviderFactories: testutil.TestAccProtoV6ProviderFactories,
45+
CheckDestroy: testAccCheckGitInstanceDestroy,
46+
Steps: []resource.TestStep{
47+
// Creation
48+
{
49+
ConfigVariables: testConfigVars,
50+
Config: testutil.GitProviderConfig() + resourceConfig,
51+
Check: resource.ComposeAggregateTestCheckFunc(
52+
resource.TestCheckResourceAttr("stackit_git.git", "project_id", testutil.ConvertConfigVariable(testConfigVars["project_id"])),
53+
resource.TestCheckResourceAttr("stackit_git.git", "name", testutil.ConvertConfigVariable(testConfigVars["name"])),
54+
resource.TestCheckResourceAttrSet("stackit_git.git", "url"),
55+
resource.TestCheckResourceAttrSet("stackit_git.git", "version"),
56+
resource.TestCheckResourceAttrSet("stackit_git.git", "instance_id"),
57+
),
58+
},
59+
// Data source
60+
{
61+
ConfigVariables: testConfigVars,
62+
Config: fmt.Sprintf(`
63+
%s
64+
65+
data "stackit_git" "git" {
66+
project_id = stackit_git.git.project_id
67+
instance_id = stackit_git.git.instance_id
68+
}
69+
`, testutil.GitProviderConfig()+resourceConfig,
70+
),
71+
Check: resource.ComposeAggregateTestCheckFunc(
72+
// Instance
73+
resource.TestCheckResourceAttr("data.stackit_git.git", "project_id", testutil.ConvertConfigVariable(testConfigVars["project_id"])),
74+
resource.TestCheckResourceAttrPair(
75+
"stackit_git.git", "project_id",
76+
"data.stackit_git.git", "project_id",
77+
),
78+
resource.TestCheckResourceAttrPair(
79+
"stackit_git.git", "instance_id",
80+
"data.stackit_git.git", "instance_id",
81+
),
82+
resource.TestCheckResourceAttrPair(
83+
"stackit_git.git", "name",
84+
"data.stackit_git.git", "name",
85+
),
86+
resource.TestCheckResourceAttrPair(
87+
"stackit_git.git", "url",
88+
"data.stackit_git.git", "url",
89+
),
90+
resource.TestCheckResourceAttrPair(
91+
"stackit_git.git", "version",
92+
"data.stackit_git.git", "version",
93+
),
94+
),
95+
},
96+
// Import
97+
{
98+
ConfigVariables: testConfigVars,
99+
ResourceName: "stackit_git.git",
100+
ImportStateIdFunc: func(s *terraform.State) (string, error) {
101+
r, ok := s.RootModule().Resources["stackit_git.git"]
102+
if !ok {
103+
return "", fmt.Errorf("couldn't find resource stackit_git.git")
104+
}
105+
instanceId, ok := r.Primary.Attributes["instance_id"]
106+
if !ok {
107+
return "", fmt.Errorf("couldn't find attribute instance_id")
108+
}
109+
return fmt.Sprintf("%s,%s", testutil.ProjectId, instanceId), nil
110+
},
111+
ImportState: true,
112+
ImportStateVerify: true,
113+
},
114+
// Update
115+
{
116+
ConfigVariables: testConfigVarsUpdated(),
117+
Config: testutil.GitProviderConfig() + resourceConfig,
118+
Check: resource.ComposeAggregateTestCheckFunc(
119+
resource.TestCheckResourceAttr("stackit_git.git", "project_id", testutil.ConvertConfigVariable(testConfigVars["project_id"])),
120+
resource.TestCheckResourceAttr("stackit_git.git", "name", testutil.ConvertConfigVariable(testConfigVarsUpdated()["name"])),
121+
resource.TestCheckResourceAttrSet("stackit_git.git", "url"),
122+
resource.TestCheckResourceAttrSet("stackit_git.git", "version"),
123+
resource.TestCheckResourceAttrSet("stackit_git.git", "instance_id"),
124+
),
125+
},
126+
// Deletion is done by the framework implicitly
127+
},
128+
})
129+
}
130+
131+
func testAccCheckGitInstanceDestroy(s *terraform.State) error {
132+
ctx := context.Background()
133+
var client *git.APIClient
134+
var err error
135+
136+
if testutil.GitCustomEndpoint == "" {
137+
client, err = git.NewAPIClient()
138+
} else {
139+
client, err = git.NewAPIClient(
140+
stackitSdkConfig.WithEndpoint(testutil.GitCustomEndpoint),
141+
)
142+
}
143+
144+
if err != nil {
145+
return fmt.Errorf("creating client: %w", err)
146+
}
147+
148+
var instancesToDestroy []string
149+
for _, rs := range s.RootModule().Resources {
150+
if rs.Type != "stackit_git" {
151+
continue
152+
}
153+
instanceId := strings.Split(rs.Primary.ID, core.Separator)[1]
154+
instancesToDestroy = append(instancesToDestroy, instanceId)
155+
}
156+
157+
instancesResp, err := client.ListInstances(ctx, testutil.ProjectId).Execute()
158+
if err != nil {
159+
return fmt.Errorf("getting git instances: %w", err)
160+
}
161+
162+
gitInstances := *instancesResp.Instances
163+
for i := range gitInstances {
164+
if gitInstances[i].Id == nil {
165+
continue
166+
}
167+
if utils.Contains(instancesToDestroy, *gitInstances[i].Id) {
168+
err := client.DeleteInstance(ctx, testutil.ProjectId, *gitInstances[i].Id).Execute()
169+
if err != nil {
170+
return fmt.Errorf("destroying git instance %s during CheckDestroy: %w", *gitInstances[i].Id, err)
171+
}
172+
}
173+
}
174+
return nil
175+
}

0 commit comments

Comments
 (0)