Skip to content

Commit eb87ef2

Browse files
authored
SIGN-32 Migrate Repository to Framework (#373)
* SIGN-32 Migrate Repository from SDKv2 to Framework * SIGN-32 Isolated conformance tests * Update provider * Update SDK provider * Added updated docs * Remove SDKv2 files * Remove isolation of conformance tests
1 parent fe219bb commit eb87ef2

12 files changed

Lines changed: 971 additions & 548 deletions

File tree

docs/data-sources/repository.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
page_title: "dbtcloud_repository Data Source - dbtcloud"
44
subcategory: ""
55
description: |-
6-
6+
Retrieve data for a single repository
77
---
88

99
# dbtcloud_repository (Data Source)
1010

11-
11+
Retrieve data for a single repository
1212

1313

1414

@@ -26,11 +26,15 @@ description: |-
2626

2727
### Read-Only
2828

29+
- `azure_active_directory_project_id` (String) The Azure Dev Ops project ID
30+
- `azure_active_directory_repository_id` (String) The Azure Dev Ops repository ID
31+
- `azure_bypass_webhook_registration_failure` (Boolean) If set to False (the default), the connection will fail if the service user doesn't have access to set webhooks
2932
- `deploy_key` (String) Public key generated by dbt when using `deploy_key` clone strategy
3033
- `git_clone_strategy` (String) Git clone strategy for the repository
3134
- `github_installation_id` (Number) Identifier for the GitHub installation
3235
- `gitlab_project_id` (Number) Identifier for the Gitlab project
33-
- `id` (String) The ID of this resource.
36+
- `id` (String) The ID of this resource
3437
- `is_active` (Boolean) Whether the repository is active
35-
- `remote_url` (String) Connection name
38+
- `pull_request_url_template` (String) The pull request URL template to be used when opening a pull request from within dbt Cloud's IDE
39+
- `remote_url` (String) Git URL for the repository or \<Group>/\<Project> for Gitlab
3640
- `repository_credentials_id` (Number) Credentials ID for the repository (From the repository side not the dbt Cloud ID)

docs/resources/repository.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
page_title: "dbtcloud_repository Resource - dbtcloud"
33
subcategory: ""
44
description: |-
5-
5+
Manages a dbt Cloud repository.
66
---
77

88
# dbtcloud_repository (Resource)
@@ -106,14 +106,14 @@ resource "dbtcloud_repository" "ado_repo" {
106106
- `fetch_deploy_key` (Boolean, Deprecated) Whether we should return the public deploy key - (for the `deploy_key` strategy)
107107
- `git_clone_strategy` (String) Git clone strategy for the repository. Can be `deploy_key` (default) for cloning via SSH Deploy Key, `github_app` for GitHub native integration, `deploy_token` for the GitLab native integration and `azure_active_directory_app` for ADO native integration
108108
- `github_installation_id` (Number) Identifier for the GitHub App - (for GitHub native integration only)
109-
- `gitlab_project_id` (Number) Identifier for the Gitlab project - (for GitLab native integration only)
109+
- `gitlab_project_id` (Number) Identifier for the Gitlab project - (for GitLab native integration only)
110110
- `is_active` (Boolean) Whether the repository is active
111-
- `pull_request_url_template` (String) URL template for creating a pull request. If it is not set, the default template will create a PR from the current branch to the branch configured in the Development environment.
111+
- `pull_request_url_template` (String) The pull request URL template to be used when opening a pull request from within dbt Cloud's IDE
112112

113113
### Read-Only
114114

115115
- `deploy_key` (String) Public key generated by dbt when using `deploy_key` clone strategy
116-
- `id` (String) The ID of this resource.
116+
- `id` (String) The ID of this resource
117117
- `repository_credentials_id` (Number) Credentials ID for the repository (From the repository side not the dbt Cloud ID)
118118
- `repository_id` (Number) Repository Identifier
119119

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
9+
"github.com/hashicorp/terraform-plugin-framework/datasource"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
)
12+
13+
// Ensure the implementation satisfies the expected interfaces
14+
var (
15+
_ datasource.DataSource = &repositoryDataSource{}
16+
_ datasource.DataSourceWithConfigure = &repositoryDataSource{}
17+
)
18+
19+
// RepositoryDataSource returns a new repository data source
20+
func RepositoryDataSource() datasource.DataSource {
21+
return &repositoryDataSource{}
22+
}
23+
24+
// repositoryDataSource is the data source implementation for repositories
25+
type repositoryDataSource struct {
26+
client *dbt_cloud.Client
27+
}
28+
29+
// Metadata returns the data source type name
30+
func (d *repositoryDataSource) Metadata(
31+
_ context.Context,
32+
req datasource.MetadataRequest,
33+
resp *datasource.MetadataResponse,
34+
) {
35+
resp.TypeName = req.ProviderTypeName + "_repository"
36+
}
37+
38+
// Schema defines the schema for the data source
39+
func (d *repositoryDataSource) Schema(
40+
_ context.Context,
41+
_ datasource.SchemaRequest,
42+
resp *datasource.SchemaResponse,
43+
) {
44+
resp.Schema = DataSourceSchema()
45+
}
46+
47+
// Read fetches the data from the API
48+
func (d *repositoryDataSource) Read(
49+
ctx context.Context,
50+
req datasource.ReadRequest,
51+
resp *datasource.ReadResponse,
52+
) {
53+
var data RepositoryDataSourceModel
54+
55+
// Read the datasource inputs
56+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
57+
if resp.Diagnostics.HasError() {
58+
return
59+
}
60+
61+
repositoryID := int(data.RepositoryID.ValueInt64())
62+
projectID := int(data.ProjectID.ValueInt64())
63+
64+
repository, err := d.client.GetRepository(
65+
strconv.Itoa(repositoryID),
66+
strconv.Itoa(projectID),
67+
)
68+
if err != nil {
69+
resp.Diagnostics.AddError(
70+
"Error getting repository",
71+
err.Error(),
72+
)
73+
return
74+
}
75+
76+
data.ID = types.StringValue(fmt.Sprintf("%d%s%d", repository.ProjectID, dbt_cloud.ID_DELIMITER, *repository.ID))
77+
data.IsActive = types.BoolValue(repository.State == dbt_cloud.STATE_ACTIVE)
78+
data.ProjectID = types.Int64Value(int64(repository.ProjectID))
79+
data.RepositoryID = types.Int64Value(int64(*repository.ID))
80+
data.RemoteURL = types.StringValue(repository.RemoteUrl)
81+
data.GitCloneStrategy = types.StringValue(repository.GitCloneStrategy)
82+
83+
if repository.RepositoryCredentialsID != nil {
84+
data.RepositoryCredentialsID = types.Int64Value(int64(*repository.RepositoryCredentialsID))
85+
} else {
86+
data.RepositoryCredentialsID = types.Int64Null()
87+
}
88+
89+
if repository.GitlabProjectID != nil {
90+
data.GitlabProjectID = types.Int64Value(int64(*repository.GitlabProjectID))
91+
} else {
92+
data.GitlabProjectID = types.Int64Null()
93+
}
94+
95+
if repository.GithubInstallationID != nil {
96+
data.GithubInstallationID = types.Int64Value(int64(*repository.GithubInstallationID))
97+
} else {
98+
data.GithubInstallationID = types.Int64Null()
99+
}
100+
101+
if repository.DeployKey != nil {
102+
data.DeployKey = types.StringValue(repository.DeployKey.PublicKey)
103+
} else {
104+
data.DeployKey = types.StringNull()
105+
}
106+
107+
if repository.PullRequestURLTemplate != "" {
108+
data.PullRequestURLTemplate = types.StringValue(repository.PullRequestURLTemplate)
109+
} else {
110+
data.PullRequestURLTemplate = types.StringNull()
111+
}
112+
113+
if repository.AzureActiveDirectoryProjectID != nil {
114+
data.AzureActiveDirectoryProjectID = types.StringValue(*repository.AzureActiveDirectoryProjectID)
115+
} else {
116+
data.AzureActiveDirectoryProjectID = types.StringNull()
117+
}
118+
119+
if repository.AzureActiveDirectoryRepositoryID != nil {
120+
data.AzureActiveDirectoryRepositoryID = types.StringValue(*repository.AzureActiveDirectoryRepositoryID)
121+
} else {
122+
data.AzureActiveDirectoryRepositoryID = types.StringNull()
123+
}
124+
125+
if repository.AzureBypassWebhookRegistrationFailure != nil {
126+
data.AzureBypassWebhookRegistrationFailure = types.BoolValue(*repository.AzureBypassWebhookRegistrationFailure)
127+
} else {
128+
data.AzureBypassWebhookRegistrationFailure = types.BoolNull()
129+
}
130+
131+
// Set the response data
132+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
133+
}
134+
135+
// Configure adds the provider configured client to the data source
136+
func (d *repositoryDataSource) Configure(
137+
_ context.Context,
138+
req datasource.ConfigureRequest,
139+
_ *datasource.ConfigureResponse,
140+
) {
141+
if req.ProviderData == nil {
142+
return
143+
}
144+
145+
d.client = req.ProviderData.(*dbt_cloud.Client)
146+
}

pkg/sdkv2/data_sources/repository_acceptance_test.go renamed to pkg/framework/objects/repository/data_source_acceptance_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package data_sources_test
1+
package repository_test
22

33
import (
44
"fmt"
@@ -10,11 +10,10 @@ import (
1010
)
1111

1212
func TestAccDbtCloudRepositoryDataSource(t *testing.T) {
13-
1413
randomProjectName := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
1514
repoUrl := "git@github.com:dbt-labs/terraform-provider-dbtcloud.git"
1615

17-
config := repository(randomProjectName, repoUrl)
16+
config := repositoryDataSourceConfig(randomProjectName, repoUrl)
1817

1918
check := resource.ComposeAggregateTestCheckFunc(
2019
resource.TestCheckResourceAttr("data.dbtcloud_repository.test", "remote_url", repoUrl),
@@ -34,7 +33,7 @@ func TestAccDbtCloudRepositoryDataSource(t *testing.T) {
3433
})
3534
}
3635

37-
func repository(projectName, repositoryUrl string) string {
36+
func repositoryDataSourceConfig(projectName, repositoryUrl string) string {
3837
return fmt.Sprintf(`
3938
resource "dbtcloud_project" "test_project" {
4039
name = "%s"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package repository
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
)
6+
7+
// RepositoryDataSourceModel represents the data source model for repositories
8+
type RepositoryDataSourceModel struct {
9+
ID types.String `tfsdk:"id"`
10+
RepositoryID types.Int64 `tfsdk:"repository_id"`
11+
ProjectID types.Int64 `tfsdk:"project_id"`
12+
IsActive types.Bool `tfsdk:"is_active"`
13+
RemoteURL types.String `tfsdk:"remote_url"`
14+
GitCloneStrategy types.String `tfsdk:"git_clone_strategy"`
15+
RepositoryCredentialsID types.Int64 `tfsdk:"repository_credentials_id"`
16+
GitlabProjectID types.Int64 `tfsdk:"gitlab_project_id"`
17+
GithubInstallationID types.Int64 `tfsdk:"github_installation_id"`
18+
DeployKey types.String `tfsdk:"deploy_key"`
19+
PullRequestURLTemplate types.String `tfsdk:"pull_request_url_template"`
20+
AzureActiveDirectoryProjectID types.String `tfsdk:"azure_active_directory_project_id"`
21+
AzureActiveDirectoryRepositoryID types.String `tfsdk:"azure_active_directory_repository_id"`
22+
AzureBypassWebhookRegistrationFailure types.Bool `tfsdk:"azure_bypass_webhook_registration_failure"`
23+
FetchDeployKey types.Bool `tfsdk:"fetch_deploy_key"`
24+
}
25+
26+
// RepositoryResourceModel represents the resource model for repositories
27+
type RepositoryResourceModel struct {
28+
ID types.String `tfsdk:"id"`
29+
RepositoryID types.Int64 `tfsdk:"repository_id"`
30+
ProjectID types.Int64 `tfsdk:"project_id"`
31+
IsActive types.Bool `tfsdk:"is_active"`
32+
RemoteURL types.String `tfsdk:"remote_url"`
33+
GitCloneStrategy types.String `tfsdk:"git_clone_strategy"`
34+
RepositoryCredentialsID types.Int64 `tfsdk:"repository_credentials_id"`
35+
GitlabProjectID types.Int64 `tfsdk:"gitlab_project_id"`
36+
GithubInstallationID types.Int64 `tfsdk:"github_installation_id"`
37+
DeployKey types.String `tfsdk:"deploy_key"`
38+
PullRequestURLTemplate types.String `tfsdk:"pull_request_url_template"`
39+
AzureActiveDirectoryProjectID types.String `tfsdk:"azure_active_directory_project_id"`
40+
AzureActiveDirectoryRepositoryID types.String `tfsdk:"azure_active_directory_repository_id"`
41+
AzureBypassWebhookRegistrationFailure types.Bool `tfsdk:"azure_bypass_webhook_registration_failure"`
42+
FetchDeployKey types.Bool `tfsdk:"fetch_deploy_key"`
43+
}

0 commit comments

Comments
 (0)