|
| 1 | +package vercel |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 11 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 12 | + "github.com/vercel/terraform-provider-vercel/client" |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure the implementation satisfies the expected interfaces. |
| 16 | +var ( |
| 17 | + _ datasource.DataSource = &deploymentDataSource{} |
| 18 | + _ datasource.DataSourceWithConfigure = &deploymentDataSource{} |
| 19 | +) |
| 20 | + |
| 21 | +func newDeploymentDataSource() datasource.DataSource { |
| 22 | + return &deploymentDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +type deploymentDataSource struct { |
| 26 | + client *client.Client |
| 27 | +} |
| 28 | + |
| 29 | +func (d *deploymentDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 30 | + resp.TypeName = req.ProviderTypeName + "_deployment" |
| 31 | +} |
| 32 | + |
| 33 | +func (d *deploymentDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 34 | + // Prevent panic if the provider has not been configured. |
| 35 | + if req.ProviderData == nil { |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + client, ok := req.ProviderData.(*client.Client) |
| 40 | + if !ok { |
| 41 | + resp.Diagnostics.AddError( |
| 42 | + "Unexpected Data Source Configure Type", |
| 43 | + fmt.Sprintf("Expected *client.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 44 | + ) |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + d.client = client |
| 49 | +} |
| 50 | + |
| 51 | +// Schema returns the schema information for an deployment data source |
| 52 | +func (r *deploymentDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 53 | + resp.Schema = schema.Schema{ |
| 54 | + Description: ` |
| 55 | +Provides information about an existing Deployment. |
| 56 | +
|
| 57 | +A Deployment is the result of building your Project and making it available through a live URL. |
| 58 | +`, |
| 59 | + Attributes: map[string]schema.Attribute{ |
| 60 | + "team_id": schema.StringAttribute{ |
| 61 | + Description: "The Team ID to the Deployment belong to. Required when reading a team resource if a default team has not been set in the provider.", |
| 62 | + Optional: true, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "id": schema.StringAttribute{ |
| 66 | + Required: true, |
| 67 | + Description: "The ID or URL of the Deployment to read.", |
| 68 | + }, |
| 69 | + "domains": schema.ListAttribute{ |
| 70 | + Description: "A list of all the domains (default domains, staging domains and production domains) that were assigned upon deployment creation.", |
| 71 | + Computed: true, |
| 72 | + ElementType: types.StringType, |
| 73 | + }, |
| 74 | + "project_id": schema.StringAttribute{ |
| 75 | + Description: "The project ID to add the deployment to.", |
| 76 | + Computed: true, |
| 77 | + }, |
| 78 | + "url": schema.StringAttribute{ |
| 79 | + Description: "A unique URL that is automatically generated for a deployment.", |
| 80 | + Computed: true, |
| 81 | + }, |
| 82 | + "production": schema.BoolAttribute{ |
| 83 | + Description: "true if the deployment is a production deployment, meaning production aliases will be assigned.", |
| 84 | + Computed: true, |
| 85 | + }, |
| 86 | + "ref": schema.StringAttribute{ |
| 87 | + Description: "The branch or commit hash that has been deployed. Note this will only work if the project is configured to use a Git repository.", |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +type DeploymentDataSource struct { |
| 95 | + Domains types.List `tfsdk:"domains"` |
| 96 | + ID types.String `tfsdk:"id"` |
| 97 | + Production types.Bool `tfsdk:"production"` |
| 98 | + ProjectID types.String `tfsdk:"project_id"` |
| 99 | + TeamID types.String `tfsdk:"team_id"` |
| 100 | + URL types.String `tfsdk:"url"` |
| 101 | + Ref types.String `tfsdk:"ref"` |
| 102 | +} |
| 103 | + |
| 104 | +func convertResponseToDeploymentDataSource(in client.DeploymentResponse) DeploymentDataSource { |
| 105 | + ref := types.StringNull() |
| 106 | + if in.GitSource.Ref != "" { |
| 107 | + ref = types.StringValue(in.GitSource.Ref) |
| 108 | + } |
| 109 | + |
| 110 | + var domains []attr.Value |
| 111 | + for _, a := range in.Aliases { |
| 112 | + domains = append(domains, types.StringValue(a)) |
| 113 | + } |
| 114 | + return DeploymentDataSource{ |
| 115 | + Domains: types.ListValueMust(types.StringType, domains), |
| 116 | + Production: types.BoolValue(in.Target != nil && *in.Target == "production"), |
| 117 | + TeamID: toTeamID(in.TeamID), |
| 118 | + ProjectID: types.StringValue(in.ProjectID), |
| 119 | + ID: types.StringValue(in.ID), |
| 120 | + URL: types.StringValue(in.URL), |
| 121 | + Ref: ref, |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Read will read the deployment information by requesting it from the Vercel API, and will update terraform |
| 126 | +// with this information. |
| 127 | +// It is called by the provider whenever data source values should be read to update state. |
| 128 | +func (d *deploymentDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 129 | + var config DeploymentDataSource |
| 130 | + diags := req.Config.Get(ctx, &config) |
| 131 | + resp.Diagnostics.Append(diags...) |
| 132 | + if resp.Diagnostics.HasError() { |
| 133 | + return |
| 134 | + } |
| 135 | + |
| 136 | + out, err := d.client.GetDeployment(ctx, config.ID.ValueString(), config.TeamID.ValueString()) |
| 137 | + if client.NotFound(err) { |
| 138 | + resp.State.RemoveResource(ctx) |
| 139 | + return |
| 140 | + } |
| 141 | + if err != nil { |
| 142 | + resp.Diagnostics.AddError( |
| 143 | + "Error reading deployment", |
| 144 | + fmt.Sprintf("Could not get deployment %s %s, unexpected error: %s", |
| 145 | + config.TeamID.ValueString(), |
| 146 | + config.ID.ValueString(), |
| 147 | + err, |
| 148 | + ), |
| 149 | + ) |
| 150 | + return |
| 151 | + } |
| 152 | + |
| 153 | + result := convertResponseToDeploymentDataSource(out) |
| 154 | + tflog.Info(ctx, "read deployment", map[string]interface{}{ |
| 155 | + "team_id": result.TeamID.ValueString(), |
| 156 | + "project_id": result.ID.ValueString(), |
| 157 | + }) |
| 158 | + |
| 159 | + diags = resp.State.Set(ctx, result) |
| 160 | + resp.Diagnostics.Append(diags...) |
| 161 | + if resp.Diagnostics.HasError() { |
| 162 | + return |
| 163 | + } |
| 164 | +} |
0 commit comments