-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from dbt-labs/awalden/migrate-azure-devops-re…
…pository-datasource chore: Migrate Azure DevOps Repository Datasource from SDKv2 to the plugin framework
- Loading branch information
Showing
7 changed files
with
255 additions
and
118 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
pkg/framework/objects/azure_dev_ops_repository/data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
pkg/framework/objects/azure_dev_ops_repository/data_source_acceptance_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
}, | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.