Skip to content

Commit

Permalink
Merge pull request #323 from dbt-labs/awalden/migrate-azure-devops-re…
Browse files Browse the repository at this point in the history
…pository-datasource

chore: Migrate Azure DevOps Repository Datasource from SDKv2 to the plugin framework
  • Loading branch information
chasewalden authored Dec 13, 2024
2 parents f7dab98 + 2f2d98c commit 44055d7
Show file tree
Hide file tree
Showing 7 changed files with 255 additions and 118 deletions.
80 changes: 80 additions & 0 deletions pkg/framework/objects/azure_dev_ops_repository/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package azure_dev_ops_repository

import (
"context"
"fmt"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/types"
)

type azureDevOpsRepositoryDataSource struct {
client *dbt_cloud.Client
}

func AzureDevOpsRepositoryDataSource() datasource.DataSource {
return &azureDevOpsRepositoryDataSource{}
}

func (d *azureDevOpsRepositoryDataSource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_azure_dev_ops_repository"
}

func (d *azureDevOpsRepositoryDataSource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var state AzureDevopsRepositoryDataSourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

repositoryName := state.Name.ValueString()
azureDevOpsProjectID := state.AzureDevOpsProjectID.ValueString()

azureDevOpsRepository, err := d.client.GetAzureDevOpsRepository(repositoryName, azureDevOpsProjectID)

if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("failed to get Azure Dev Ops repository %s in project %s", repositoryName, azureDevOpsProjectID),
err.Error(),
)
}

state.ID = types.StringValue(azureDevOpsRepository.ID)
state.Name = types.StringValue(azureDevOpsRepository.Name)
state.AzureDevOpsProjectID = types.StringValue(azureDevOpsProjectID)
state.DetailsURL = types.StringValue(azureDevOpsRepository.DetailsURL)
state.RemoteURL = types.StringValue(azureDevOpsRepository.RemoteURL)
state.WebURL = types.StringValue(azureDevOpsRepository.WebURL)
state.DefaultBranch = types.StringValue(azureDevOpsRepository.DefaultBranch)

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}
}

func (d *azureDevOpsRepositoryDataSource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
switch c := req.ProviderData.(type) {
case nil: // do nothing
case *dbt_cloud.Client:
d.client = c
default:
resp.Diagnostics.AddError(
"Missing client",
"A client is required to configure the Azure DevOps repository data source",
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package azure_dev_ops_repository_test

import (
"os"
"testing"

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/acctest_helper"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccDbtCloudAzureDevOpsRepository(t *testing.T) {
//TODO: Remove both env var checks when this gets configurted in CI and the variables are parameterized
if os.Getenv("CI") != "" {
t.Skip("Skipping Azure DevOps Repository datasource test in CI " +
"until Azure integration and a personal access token are available")
}

if os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN") == "" {
t.Skip("Skipping Azure DevOps Repository datasource because no personal access token is available")
}

//TODO: Parameterize these values when a standard method is available for parameterization
adoProjectName := "dbt-cloud-ado-project"
adoRepoName := "dbt-cloud-ado-repo"
personalAccessToken := os.Getenv("DBT_CLOUD_PERSONAL_ACCESS_TOKEN")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest_helper.TestAccPreCheck(t) },

ProtoV6ProviderFactories: acctest_helper.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
ConfigVariables: config.Variables{
"dbt_token": config.StringVariable(personalAccessToken),
"ado_project_name": config.StringVariable(adoProjectName),
"ado_repository_name": config.StringVariable(adoRepoName),
},
Config: `
variable "dbt_token" {
type = string
sensitive = true
}
provider "dbtcloud" {
token = var.dbt_token
}
variable "ado_project_name" {
type = string
}
variable "ado_repository_name" {
type = string
}
data dbtcloud_azure_dev_ops_project test {
name = var.ado_project_name
}
data dbtcloud_azure_dev_ops_repository test {
name = var.ado_repository_name
azure_dev_ops_project_id = data.dbtcloud_azure_dev_ops_project.test.id
}
`,
// we check the computed values, for the other ones the test suite already checks that the plan and state are the same
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(
"data.dbtcloud_azure_dev_ops_repository.test",
"id",
),
resource.TestCheckResourceAttrSet(
"data.dbtcloud_azure_dev_ops_repository.test",
"details_url",
),
resource.TestCheckResourceAttrSet(
"data.dbtcloud_azure_dev_ops_repository.test",
"remote_url",
),
resource.TestCheckResourceAttrSet(
"data.dbtcloud_azure_dev_ops_repository.test",
"web_url",
),
resource.TestCheckResourceAttrSet(
"data.dbtcloud_azure_dev_ops_repository.test",
"default_branch",
),
),
},
},
})

}
13 changes: 13 additions & 0 deletions pkg/framework/objects/azure_dev_ops_repository/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package azure_dev_ops_repository

import "github.com/hashicorp/terraform-plugin-framework/types"

type AzureDevopsRepositoryDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
AzureDevOpsProjectID types.String `tfsdk:"azure_dev_ops_project_id"`
DetailsURL types.String `tfsdk:"details_url"`
RemoteURL types.String `tfsdk:"remote_url"`
WebURL types.String `tfsdk:"web_url"`
DefaultBranch types.String `tfsdk:"default_branch"`
}
52 changes: 52 additions & 0 deletions pkg/framework/objects/azure_dev_ops_repository/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package azure_dev_ops_repository

import (
"context"

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

func (d *azureDevOpsRepositoryDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: `Use this data source to retrieve the ID and details of an Azure Dev Ops repository
based on its name and the ID of the Azure Dev Ops project it belongs to.
This data source requires connecting with a user token and doesn't work with a service token.`,
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Description: "The internal Azure Dev Ops ID of the ADO Repository",
},
"name": schema.StringAttribute{
Required: true,
Description: "The name of the ADO repository",
},
"azure_dev_ops_project_id": schema.StringAttribute{
Required: true,
Description: "The internal Azure Dev Ops ID of the ADO Project. Can be retrieved using the data source dbtcloud_azure_dev_ops_project and the project name",
},
"details_url": schema.StringAttribute{
Computed: true,
Description: "The URL of the ADO repository showing details about the repository and its attributes",
},
"remote_url": schema.StringAttribute{
Computed: true,
Description: "The HTTP URL of the ADO repository used to connect to dbt Cloud",
},
"web_url": schema.StringAttribute{
Computed: true,
Description: "The URL of the ADO repository accessible in the browser",
},
"default_branch": schema.StringAttribute{
Computed: true,
Description: "The default branch of the ADO repository",
},
},
}

}
2 changes: 2 additions & 0 deletions pkg/provider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/dbt_cloud"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/account_features"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/azure_dev_ops_repository"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/environment"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/global_connection"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/group"
Expand Down Expand Up @@ -183,6 +184,7 @@ func (p *dbtCloudProvider) Configure(
func (p *dbtCloudProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
azure_dev_ops_project.AzureDevOpsProjectDataSource,
azure_dev_ops_repository.AzureDevOpsRepositoryDataSource,
user.UserDataSource,
user.UsersDataSource,
notification.NotificationDataSource,
Expand Down
31 changes: 15 additions & 16 deletions pkg/provider/sdk_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,21 @@ func SDKProvider(version string) func() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"dbtcloud_job": data_sources.DatasourceJob(),
"dbtcloud_project": data_sources.DatasourceProject(),
"dbtcloud_environment_variable": data_sources.DatasourceEnvironmentVariable(),
"dbtcloud_snowflake_credential": data_sources.DatasourceSnowflakeCredential(),
"dbtcloud_bigquery_credential": data_sources.DatasourceBigQueryCredential(),
"dbtcloud_postgres_credential": data_sources.DatasourcePostgresCredential(),
"dbtcloud_databricks_credential": data_sources.DatasourceDatabricksCredential(),
"dbtcloud_connection": data_sources.DatasourceConnection(),
"dbtcloud_bigquery_connection": data_sources.DatasourceBigQueryConnection(),
"dbtcloud_repository": data_sources.DatasourceRepository(),
"dbtcloud_webhook": data_sources.DatasourceWebhook(),
"dbtcloud_privatelink_endpoint": data_sources.DatasourcePrivatelinkEndpoint(),
"dbtcloud_user_groups": data_sources.DatasourceUserGroups(),
"dbtcloud_extended_attributes": data_sources.DatasourceExtendedAttributes(),
"dbtcloud_group_users": data_sources.DatasourceGroupUsers(),
"dbtcloud_azure_dev_ops_repository": data_sources.DatasourceAzureDevOpsRepository(),
"dbtcloud_job": data_sources.DatasourceJob(),
"dbtcloud_project": data_sources.DatasourceProject(),
"dbtcloud_environment_variable": data_sources.DatasourceEnvironmentVariable(),
"dbtcloud_snowflake_credential": data_sources.DatasourceSnowflakeCredential(),
"dbtcloud_bigquery_credential": data_sources.DatasourceBigQueryCredential(),
"dbtcloud_postgres_credential": data_sources.DatasourcePostgresCredential(),
"dbtcloud_databricks_credential": data_sources.DatasourceDatabricksCredential(),
"dbtcloud_connection": data_sources.DatasourceConnection(),
"dbtcloud_bigquery_connection": data_sources.DatasourceBigQueryConnection(),
"dbtcloud_repository": data_sources.DatasourceRepository(),
"dbtcloud_webhook": data_sources.DatasourceWebhook(),
"dbtcloud_privatelink_endpoint": data_sources.DatasourcePrivatelinkEndpoint(),
"dbtcloud_user_groups": data_sources.DatasourceUserGroups(),
"dbtcloud_extended_attributes": data_sources.DatasourceExtendedAttributes(),
"dbtcloud_group_users": data_sources.DatasourceGroupUsers(),
},
ResourcesMap: map[string]*schema.Resource{
"dbtcloud_job": resources.ResourceJob(),
Expand Down
102 changes: 0 additions & 102 deletions pkg/sdkv2/data_sources/azure_dev_ops_repository.go

This file was deleted.

0 comments on commit 44055d7

Please sign in to comment.