|
| 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 | +} |
0 commit comments