|
| 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/v3/client" |
| 13 | +) |
| 14 | + |
| 15 | +// Ensure the implementation satisfies the expected interfaces. |
| 16 | +var ( |
| 17 | + _ datasource.DataSource = &domainConfigDataSource{} |
| 18 | + _ datasource.DataSourceWithConfigure = &domainConfigDataSource{} |
| 19 | +) |
| 20 | + |
| 21 | +func newDomainConfigDataSource() datasource.DataSource { |
| 22 | + return &domainConfigDataSource{} |
| 23 | +} |
| 24 | + |
| 25 | +type domainConfigDataSource struct { |
| 26 | + client *client.Client |
| 27 | +} |
| 28 | + |
| 29 | +func (d *domainConfigDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 30 | + resp.TypeName = req.ProviderTypeName + "_domain_config" |
| 31 | +} |
| 32 | + |
| 33 | +func (d *domainConfigDataSource) 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 a domain config data source |
| 52 | +func (d *domainConfigDataSource) Schema(_ context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 53 | + resp.Schema = schema.Schema{ |
| 54 | + Description: ` |
| 55 | +Provides domain configuration information for a Vercel project. |
| 56 | +
|
| 57 | +This data source returns configuration details for a domain associated with a specific project, |
| 58 | +including recommended CNAME and IPv4 values. |
| 59 | + `, |
| 60 | + Attributes: map[string]schema.Attribute{ |
| 61 | + "domain": schema.StringAttribute{ |
| 62 | + Required: true, |
| 63 | + Description: "The domain name to get configuration for.", |
| 64 | + }, |
| 65 | + "project_id_or_name": schema.StringAttribute{ |
| 66 | + Required: true, |
| 67 | + Description: "The project ID or name associated with the domain.", |
| 68 | + }, |
| 69 | + "team_id": schema.StringAttribute{ |
| 70 | + Optional: true, |
| 71 | + Computed: true, |
| 72 | + Description: "The ID of the team the domain config exists under. Required when configuring a team resource if a default team has not been set in the provider.", |
| 73 | + }, |
| 74 | + "recommended_cname": schema.StringAttribute{ |
| 75 | + Computed: true, |
| 76 | + Description: "The recommended CNAME value for the domain.", |
| 77 | + }, |
| 78 | + "recommended_ipv4s": schema.ListAttribute{ |
| 79 | + ElementType: types.StringType, |
| 80 | + Computed: true, |
| 81 | + Description: "The recommended IPv4 values for the domain.", |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// DomainConfigDataSource reflects the state terraform stores internally for a domain config. |
| 88 | +type DomainConfigDataSource struct { |
| 89 | + Domain types.String `tfsdk:"domain"` |
| 90 | + ProjectIdOrName types.String `tfsdk:"project_id_or_name"` |
| 91 | + TeamID types.String `tfsdk:"team_id"` |
| 92 | + RecommendedCNAME types.String `tfsdk:"recommended_cname"` |
| 93 | + RecommendedIPv4s types.List `tfsdk:"recommended_ipv4s"` |
| 94 | +} |
| 95 | + |
| 96 | +// Read will read domain config information by requesting it from the Vercel API, and will update terraform |
| 97 | +// with this information. |
| 98 | +// It is called by the provider whenever data source values should be read to update state. |
| 99 | +func (d *domainConfigDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { |
| 100 | + var config DomainConfigDataSource |
| 101 | + diags := req.Config.Get(ctx, &config) |
| 102 | + resp.Diagnostics.Append(diags...) |
| 103 | + if resp.Diagnostics.HasError() { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + out, err := d.client.GetDomainConfig(ctx, config.Domain.ValueString(), config.ProjectIdOrName.ValueString(), config.TeamID.ValueString()) |
| 108 | + if err != nil { |
| 109 | + resp.Diagnostics.AddError( |
| 110 | + "Error reading domain config", |
| 111 | + fmt.Sprintf("Could not read domain config for domain %s and project %s, unexpected error: %s", |
| 112 | + config.Domain.ValueString(), |
| 113 | + config.ProjectIdOrName.ValueString(), |
| 114 | + err, |
| 115 | + ), |
| 116 | + ) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + var ipv4Values []attr.Value |
| 121 | + for _, ip := range out.RecommendedIPv4s { |
| 122 | + ipv4Values = append(ipv4Values, types.StringValue(ip)) |
| 123 | + } |
| 124 | + |
| 125 | + result := DomainConfigDataSource{ |
| 126 | + Domain: config.Domain, |
| 127 | + ProjectIdOrName: config.ProjectIdOrName, |
| 128 | + TeamID: config.TeamID, |
| 129 | + RecommendedCNAME: types.StringValue(out.RecommendedCNAME), |
| 130 | + RecommendedIPv4s: types.ListValueMust(types.StringType, ipv4Values), |
| 131 | + } |
| 132 | + |
| 133 | + tflog.Info(ctx, "read domain config", map[string]any{ |
| 134 | + "domain": result.Domain.ValueString(), |
| 135 | + "projectIdOrName": result.ProjectIdOrName.ValueString(), |
| 136 | + "teamId": result.TeamID.ValueString(), |
| 137 | + "recommendedCNAME": result.RecommendedCNAME.ValueString(), |
| 138 | + "recommendedIPv4s": result.RecommendedIPv4s.Elements(), |
| 139 | + }) |
| 140 | + |
| 141 | + diags = resp.State.Set(ctx, result) |
| 142 | + resp.Diagnostics.Append(diags...) |
| 143 | + if resp.Diagnostics.HasError() { |
| 144 | + return |
| 145 | + } |
| 146 | +} |
0 commit comments