Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion docs/data-sources/site.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data "netlify_site" "blog" {
### Optional

- `name` (String)
- `team_slug` (String)
- `team_slug` (String) Required if name is specified and a default team was not configured in the provider configuration.

### Read-Only

Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/sites.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ data "netlify_sites" "team" {
<!-- schema generated by tfplugindocs -->
## Schema

### Required
### Optional

- `team_slug` (String)
- `team_slug` (String) Required if a default team was not configured in the provider configuration.

### Read-Only

Expand Down
4 changes: 2 additions & 2 deletions docs/data-sources/team.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ data "netlify_team" "team" {

### Optional

- `slug` (String)
- `id` (String) ID or slug are required if a default team was not configured in the provider configuration.
- `slug` (String) ID or slug are required if a default team was not configured in the provider configuration.

### Read-Only

- `id` (String) The ID of this resource.
- `name` (String)
12 changes: 8 additions & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ terraform {

provider "netlify" {
token = var.netlify_api_token
# Optionally, set a default team through its ID or its slug to avoid repeating it.
default_team_slug = "your-team-slug"
}

data "netlify_team" "team" {
slug = "your-team-slug"
# slug coming from the default team
}

data "netlify_site" "blog" {
team_slug = data.netlify_team.team.slug
name = "blog"
# team_slug coming from the default team
name = "blog"
}

resource "netlify_environment_variable" "astro_database_file" {
team_id = data.netlify_team.team.id
# team_id coming from the default team
site_id = data.netlify_site.blog.id
key = "ASTRO_DATABASE_FILE"
values = [
Expand All @@ -77,5 +79,7 @@ resource "netlify_environment_variable" "astro_database_file" {

### Optional

- `default_team_id` (String) The default team ID to use for resources that require a team ID or a team slug. Warning: Changing this value may not trigger recreation of resources.
- `default_team_slug` (String) The default team slug to use for resources that require a team ID or a team slug. Warning: Changing this value may not trigger recreation of resources.
- `endpoint` (String) Defaults to: https://api.netlify.com
- `token` (String, Sensitive) Read: https://docs.netlify.com/api/get-started/#authentication , will use the `NETLIFY_API_TOKEN` environment variable if not set.
5 changes: 4 additions & 1 deletion docs/resources/dns_zone.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ resource "netlify_dns_zone" "example" {
### Required

- `name` (String)
- `team_slug` (String)

### Optional

- `team_slug` (String) Required if a default team was not configured in the provider configuration.

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion docs/resources/environment_variable.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ resource "netlify_environment_variable" "astro_database_file" {
### Required

- `key` (String)
- `team_id` (String)

### Optional

- `scopes` (Set of String) One or more of builds, functions, runtime, and post-processing
- `secret_values` (Attributes Set) (see [below for nested schema](#nestedatt--secret_values))
- `site_id` (String)
- `team_id` (String) Required if a default team was not configured in the provider configuration.
- `values` (Attributes Set) (see [below for nested schema](#nestedatt--values))

### Read-Only
Expand Down
5 changes: 4 additions & 1 deletion docs/resources/team_firewall_traffic_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ resource "netlify_team_firewall_traffic_rules" "team" {
### Required

- `published` (Attributes) (see [below for nested schema](#nestedatt--published))
- `team_id` (String)
- `unpublished` (Attributes) (see [below for nested schema](#nestedatt--unpublished))

### Optional

- `team_id` (String) Required if a default team was not configured in the provider configuration.

### Read-Only

- `last_updated` (String)
Expand Down
11 changes: 4 additions & 7 deletions examples/misc/dns/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ terraform {
}

# `token` comes from NETLIFY_API_TOKEN, but can be specified with a Terraform variable
provider "netlify" {}

# data "netlify_team" "current" {
# slug = "ramon-test-1"
# }
provider "netlify" {
default_team_slug = "ramon-test-1"
}

resource "netlify_dns_zone" "example" {
team_slug = "ramon-test-1" // data.netlify_team.current.slug
name = "example-tf-test-test.com"
name = "example-tf-test-test.com"
lifecycle {
prevent_destroy = true
}
Expand Down
15 changes: 14 additions & 1 deletion examples/misc/env_vars/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ terraform {
}

# `token` comes from NETLIFY_API_TOKEN, but can be specified with a Terraform variable
provider "netlify" {}
provider "netlify" {
default_team_slug = "ramon-test-1"
}

data "netlify_team" "current" {
slug = "ramon-test-1"
Expand All @@ -31,6 +33,17 @@ resource "netlify_environment_variable" "woof" {
]
}

resource "netlify_environment_variable" "woof2" {
site_id = data.netlify_site.platform_test.id
key = "WOOF2"
values = [
{
value = "dogs are here",
context = "all",
}
]
}

resource "netlify_environment_variable" "meow" {
team_id = data.netlify_team.current.id
site_id = data.netlify_site.platform_test.id
Expand Down
17 changes: 9 additions & 8 deletions examples/misc/site_data_sources/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ terraform {
}

# `token` comes from NETLIFY_API_TOKEN, but can be specified with a Terraform variable
provider "netlify" {}

data "netlify_team" "current" {
slug = "ramon-test-1"
provider "netlify" {
default_team_slug = "ramon-test-1"
}

data "netlify_team" "current" {}

data "netlify_site" "platform_test" {
team_slug = data.netlify_team.current.slug
name = "platform-test-1"
name = "platform-test-1"
}

data "netlify_sites" "all" {
data "netlify_sites" "mine" {}

data "netlify_sites" "testing" {
team_slug = "netlify-testing"
}

output "sites" {
value = [
for site in data.netlify_sites.all.sites : site
for site in data.netlify_sites.testing.sites : site
if site.custom_domain != ""
]
}
10 changes: 6 additions & 4 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ terraform {

provider "netlify" {
token = var.netlify_api_token
# Optionally, set a default team through its ID or its slug to avoid repeating it.
default_team_slug = "your-team-slug"
}

data "netlify_team" "team" {
slug = "your-team-slug"
# slug coming from the default team
}

data "netlify_site" "blog" {
team_slug = data.netlify_team.team.slug
name = "blog"
# team_slug coming from the default team
name = "blog"
}

resource "netlify_environment_variable" "astro_database_file" {
team_id = data.netlify_team.team.id
# team_id coming from the default team
site_id = data.netlify_site.blog.id
key = "ASTRO_DATABASE_FILE"
values = [
Expand Down
27 changes: 21 additions & 6 deletions internal/provider/dns_zone_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand Down Expand Up @@ -89,9 +90,12 @@ func (r *dnsZoneResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
},
"team_slug": schema.StringAttribute{
Required: true,
Computed: true,
Optional: true,
Description: "Required if a default team was not configured in the provider configuration.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.RequiresReplaceIfConfigured(),
stringplanmodifier.UseStateForUnknown(),
},
},
"dns_servers": schema.ListAttribute{
Expand All @@ -102,7 +106,8 @@ func (r *dnsZoneResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},
},
"domain": schema.SingleNestedAttribute{
Computed: true,
Computed: true,
PlanModifiers: []planmodifier.Object{objectplanmodifier.UseStateForUnknown()},
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
Expand Down Expand Up @@ -138,10 +143,19 @@ func (r *dnsZoneResource) Create(ctx context.Context, req resource.CreateRequest
return
}

teamSlug := r.data.teamSlugOrDefault(plan.TeamSlug)
if teamSlug == nil {
resp.Diagnostics.AddError(
"Missing team slug",
"Team slug is required for creating a Netlify DNS zone. Please provide a team slug in the plan or configure a default team in the provider configuration.",
)
return
}

dnsZone, _, err := r.data.client.DNSZonesAPI.
CreateDnsZone(ctx).
DnsZoneCreateParams(netlifyapi.DnsZoneCreateParams{
AccountSlug: plan.TeamSlug.ValueStringPointer(),
AccountSlug: teamSlug,
Name: plan.Name.ValueStringPointer(),
}).
Execute()
Expand All @@ -151,7 +165,7 @@ func (r *dnsZoneResource) Create(ctx context.Context, req resource.CreateRequest
fmt.Sprintf(
"Could not create Netlify DNS zone %q (team slug: %q): %q",
plan.Name.ValueString(),
plan.TeamSlug.ValueString(),
*teamSlug,
err.Error(),
),
)
Expand All @@ -160,6 +174,7 @@ func (r *dnsZoneResource) Create(ctx context.Context, req resource.CreateRequest
plan.ID = types.StringValue(dnsZone.Id)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC3339))
plan.TeamID = types.StringValue(dnsZone.AccountId)
plan.TeamSlug = types.StringValue(dnsZone.AccountSlug)
dnsServers := make([]types.String, len(dnsZone.DnsServers))
for i, dnsServer := range dnsZone.DnsServers {
dnsServers[i] = types.StringValue(dnsServer)
Expand Down Expand Up @@ -249,7 +264,7 @@ func (r *dnsZoneResource) Read(ctx context.Context, req resource.ReadRequest, re
}

func (r *dnsZoneResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError(
resp.Diagnostics.AddWarning(
"Update not supported for Netlify DNS zones",
"Update is not supported for Netlify DNS zones at this time.",
)
Expand Down
30 changes: 24 additions & 6 deletions internal/provider/environment_variable_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@ func (r *environmentVariableResource) Schema(_ context.Context, _ resource.Schem
Computed: true,
},
"team_id": schema.StringAttribute{
Required: true,
Computed: true,
Optional: true,
Description: "Required if a default team was not configured in the provider configuration.",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
stringplanmodifier.RequiresReplaceIfConfigured(),
stringplanmodifier.UseStateForUnknown(),
},
},
"site_id": schema.StringAttribute{
Expand Down Expand Up @@ -188,6 +191,15 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
return
}

teamId := r.data.teamIdOrDefault(plan.TeamID)
if teamId == nil {
resp.Diagnostics.AddError(
"Missing team ID",
"Team ID is required for creating a Netlify environment variable. Please provide a team ID in the plan or configure a default team in the provider configuration.",
)
return
}

scopes := make([]string, len(plan.Scopes))
for i, scope := range plan.Scopes {
scopes[i] = scope.ValueString()
Expand All @@ -202,7 +214,7 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
isSecret = false
}
createEnvVars := r.data.client.EnvironmentVariablesAPI.
CreateEnvVars(ctx, plan.TeamID.ValueString()).
CreateEnvVars(ctx, *teamId).
EnvVar([]netlifyapi.EnvVar{
{
Key: plan.Key.ValueString(),
Expand All @@ -221,7 +233,7 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
fmt.Sprintf(
"Could not create Netlify environment variable order ID %q (team ID: %q, site ID: %q, secret: %v): %q",
plan.Key.ValueString(),
plan.TeamID.ValueString(),
*teamId,
plan.SiteID.ValueString(),
isSecret,
err.Error(),
Expand All @@ -230,6 +242,7 @@ func (r *environmentVariableResource) Create(ctx context.Context, req resource.C
return
}
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC3339))
plan.TeamID = types.StringValue(*teamId)

resp.Diagnostics.Append(resp.State.Set(ctx, plan)...)
if resp.Diagnostics.HasError() {
Expand Down Expand Up @@ -283,6 +296,11 @@ func (r *environmentVariableResource) Update(ctx context.Context, req resource.U
if resp.Diagnostics.HasError() {
return
}
var state environmentVariableResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}

scopes := make([]string, len(plan.Scopes))
for i, scope := range plan.Scopes {
Expand All @@ -298,7 +316,7 @@ func (r *environmentVariableResource) Update(ctx context.Context, req resource.U
isSecret = false
}
updateEnvVar := r.data.client.EnvironmentVariablesAPI.
UpdateEnvVar(ctx, plan.TeamID.ValueString(), plan.Key.ValueString()).
UpdateEnvVar(ctx, state.TeamID.ValueString(), plan.Key.ValueString()).
Key(plan.Key.ValueString()).UpdateEnvVarRequest(netlifyapi.UpdateEnvVarRequest{
Scopes: scopes,
Values: values,
Expand All @@ -314,7 +332,7 @@ func (r *environmentVariableResource) Update(ctx context.Context, req resource.U
fmt.Sprintf(
"Could not update Netlify environment variable order ID %q (team ID: %q, site ID: %q, secret: %v): %q",
plan.Key.ValueString(),
plan.TeamID.ValueString(),
state.TeamID.ValueString(),
plan.SiteID.ValueString(),
isSecret,
err.Error(),
Expand Down
Loading