Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate Azure DevOps Project Datasource from SDKv2 to the plugin framework #321

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions pkg/framework/objects/azure_dev_ops_project/data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package azure_dev_ops_project

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"
)

var (
_ datasource.DataSource = &azureDevOpsProjectDataSource{}
_ datasource.DataSourceWithConfigure = &azureDevOpsProjectDataSource{}
)

func AzureDevOpsProjectDataSource() datasource.DataSource {
return &azureDevOpsProjectDataSource{}
}

type azureDevOpsProjectDataSource struct {
client *dbt_cloud.Client
}

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

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

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

projectName := state.Name.ValueString()

azureDevOpsProject, err := d.client.GetAzureDevOpsProject(projectName)

if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf("Did not find Azure DevOps Project with name: %s", state.Name),
err.Error(),
)
return
}

state.Name = types.StringValue(azureDevOpsProject.Name)
state.ID = types.StringValue(azureDevOpsProject.ID)
state.URL = types.StringValue(azureDevOpsProject.URL)

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (d *azureDevOpsProjectDataSource) 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 Project data source",
)
}
}
9 changes: 9 additions & 0 deletions pkg/framework/objects/azure_dev_ops_project/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package azure_dev_ops_project

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

type AzureDevOpsProjectDataSourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
URL types.String `tfsdk:"url"`
}
35 changes: 35 additions & 0 deletions pkg/framework/objects/azure_dev_ops_project/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package azure_dev_ops_project

import (
"context"

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

func (r *azureDevOpsProjectDataSource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: `Use this data source to retrieve the ID of an Azure Dev Ops project
based on its name.

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 Project",
},
"name": schema.StringAttribute{
Required: true,
Description: "The name of the ADO project",
},
"url": schema.StringAttribute{
Computed: true,
Description: "The URL of the ADO project",
},
},
}
}
2 changes: 2 additions & 0 deletions pkg/provider/framework_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"
"github.com/dbt-labs/terraform-provider-dbtcloud/pkg/framework/objects/azure_dev_ops_project"
"os"
"strconv"

Expand Down Expand Up @@ -181,6 +182,7 @@ func (p *dbtCloudProvider) Configure(

func (p *dbtCloudProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
azure_dev_ops_project.AzureDevOpsProjectDataSource,
user.UserDataSource,
user.UsersDataSource,
notification.NotificationDataSource,
Expand Down
1 change: 0 additions & 1 deletion pkg/provider/sdk_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func SDKProvider(version string) func() *schema.Provider {
"dbtcloud_user_groups": data_sources.DatasourceUserGroups(),
"dbtcloud_extended_attributes": data_sources.DatasourceExtendedAttributes(),
"dbtcloud_group_users": data_sources.DatasourceGroupUsers(),
"dbtcloud_azure_dev_ops_project": data_sources.DatasourceAzureDevOpsProject(),
"dbtcloud_azure_dev_ops_repository": data_sources.DatasourceAzureDevOpsRepository(),
},
ResourcesMap: map[string]*schema.Resource{
Expand Down
69 changes: 0 additions & 69 deletions pkg/sdkv2/data_sources/azure_dev_ops_project.go

This file was deleted.

Loading