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

New resource: azurerm_mssql_job_target_group #28492

Merged
merged 12 commits into from
Jan 30, 2025
Prev Previous commit
Next Next commit
Minor fixes
  • Loading branch information
sreallymatt committed Jan 14, 2025
commit 94d89bac28701203f133fb5e2e4032b6e5be76eb
74 changes: 37 additions & 37 deletions internal/services/mssql/mssql_job_target_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,30 @@ import (
type MsSqlJobTargetGroupResource struct{}

type MsSqlJobTargetGroupResourceModel struct {
Name string `tfschema:"name"`
JobAgentID string `tfschema:"job_agent_id"`
JobTargets []MsSqlJobTarget `tfschema:"job_target"`
Name string `tfschema:"name"`
}

type MsSqlJobTarget struct {
ServerName string `tfschema:"server_name"`
Type string `tfschema:"type"`
DatabaseName string `tfschema:"database_name"`
ElasticPoolName string `tfschema:"elastic_pool_name"`
MembershipType string `tfschema:"membership_type"`
JobCredentialId string `tfschema:"job_credential_id"`
ServerName string `tfschema:"server_name"`
Type string `tfschema:"type"`
MembershipType string `tfschema:"membership_type"`
}

var _ sdk.ResourceWithUpdate = MsSqlJobTargetGroupResource{}

func (r MsSqlJobTargetGroupResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ForceNew: true,
},
"job_agent_id": {
Type: pluginsdk.TypeString,
Required: true,
Expand All @@ -48,6 +54,20 @@ func (r MsSqlJobTargetGroupResource) Arguments() map[string]*pluginsdk.Schema {
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"server_name": {
sreallymatt marked this conversation as resolved.
Show resolved Hide resolved
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ValidateMsSqlServerName,
},
"type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(jobtargetgroups.JobTargetTypeSqlDatabase),
string(jobtargetgroups.JobTargetTypeSqlElasticPool),
string(jobtargetgroups.JobTargetTypeSqlServer),
}, false),
},
"database_name": {
Type: pluginsdk.TypeString,
Optional: true,
Expand All @@ -58,40 +78,20 @@ func (r MsSqlJobTargetGroupResource) Arguments() map[string]*pluginsdk.Schema {
Optional: true,
ValidateFunc: validate.ValidateMsSqlElasticPoolName,
},
"membership_type": {
Type: pluginsdk.TypeString,
Optional: true,
Default: string(jobtargetgroups.JobTargetGroupMembershipTypeInclude),
ValidateFunc: validation.StringInSlice(jobtargetgroups.PossibleValuesForJobTargetGroupMembershipType(), false),
},
"job_credential_id": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: jobcredentials.ValidateCredentialID,
},
"server_name": {
"membership_type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validate.ValidateMsSqlServerName,
},
"type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(jobtargetgroups.JobTargetTypeSqlDatabase),
string(jobtargetgroups.JobTargetTypeSqlElasticPool),
string(jobtargetgroups.JobTargetTypeSqlServer),
}, false),
Optional: true,
Default: string(jobtargetgroups.JobTargetGroupMembershipTypeInclude),
ValidateFunc: validation.StringInSlice(jobtargetgroups.PossibleValuesForJobTargetGroupMembershipType(), false),
},
},
},
},
"name": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
ForceNew: true,
},
}
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func (r MsSqlJobTargetGroupResource) Create() sdk.ResourceFunc {

existing, err := client.Get(ctx, id)
if err != nil && !response.WasNotFound(existing.HttpResponse) {
return fmt.Errorf("checking for presence of existing %s: %+v", id.ID(), err)
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
}

if !response.WasNotFound(existing.HttpResponse) {
Expand All @@ -147,7 +147,7 @@ func (r MsSqlJobTargetGroupResource) Create() sdk.ResourceFunc {
}

if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil {
return fmt.Errorf("creating %s: %+v", id.ID(), err)
return fmt.Errorf("creating %s: %+v", id, err)
}

metadata.SetID(id)
Expand All @@ -173,7 +173,7 @@ func (r MsSqlJobTargetGroupResource) Read() sdk.ResourceFunc {
return metadata.MarkAsGone(id)
}

return fmt.Errorf("retrieving %s: %+v", id.ID(), err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

state := MsSqlJobTargetGroupResourceModel{
Expand Down Expand Up @@ -205,20 +205,20 @@ func (r MsSqlJobTargetGroupResource) Update() sdk.ResourceFunc {

var config MsSqlJobTargetGroupResourceModel
if err := metadata.Decode(&config); err != nil {
return fmt.Errorf("decoding %s: %+v", id.ID(), err)
return fmt.Errorf("decoding %+v", err)
}

existing, err := client.Get(ctx, *id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id.ID(), err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

if existing.Model == nil {
return fmt.Errorf("retrieving %s: `model` was nil", id.ID())
return fmt.Errorf("retrieving %s: `model` was nil", id)
}

if existing.Model.Properties == nil {
return fmt.Errorf("retrieving %s: `model.Properties` was nil", id.ID())
return fmt.Errorf("retrieving %s: `model.Properties` was nil", id)
}

if metadata.ResourceData.HasChange("job_target") {
Expand All @@ -230,7 +230,7 @@ func (r MsSqlJobTargetGroupResource) Update() sdk.ResourceFunc {
}

if _, err := client.CreateOrUpdate(ctx, *id, *existing.Model); err != nil {
return fmt.Errorf("updating: %s: %+v", id.ID(), err)
return fmt.Errorf("updating: %s: %+v", id, err)
}

return nil
Expand All @@ -250,7 +250,7 @@ func (r MsSqlJobTargetGroupResource) Delete() sdk.ResourceFunc {
}

if _, err := client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %+v", id.ID(), err)
return fmt.Errorf("deleting %s: %+v", id, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ func TestAccMsSqlJobTargetGroupTest_requiresImport(t *testing.T) {

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.requiresImport(data),
ExpectError: acceptance.RequiresImportError("azurerm_mssql_job_target_group"),
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.RequiresImportErrorStep(r.requiresImport),
})
}

Expand Down
8 changes: 4 additions & 4 deletions website/docs/r/mssql_job_target_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ resource "azurerm_mssql_job_target_group" "example" {

The following arguments are supported:

* `job_agent_id` - (Required) The ID of the Elastic Job Agent. Changing this forces a new Job Target Group to be created.

* `name` - (Required) The name which should be used for this Job Target Group. Changing this forces a new Job Target Group to be created.

* `job_agent_id` - (Required) The ID of the Elastic Job Agent. Changing this forces a new Job Target Group to be created.

---

* `job_target` - (Optional) One or more `job_target` blocks as defined below.
Expand All @@ -87,10 +87,10 @@ A `job_target` block supports the following:

~> **Note:** This is required when `type` is `SqlElasticPool`.

* `membership_type` - (Optional) The membership type for this job target. Possible values are `Include` and `Exclude`. Defaults to `Include`.

* `job_credential_id` - (Optional) The ID of the job credential to use during execution of jobs.

* `membership_type` - (Optional) The membership type for this job target. Possible values are `Include` and `Exclude`. Defaults to `Include`.

~> **Note:** This is required when `membership_type` is `Include`, unless `type` is `SqlDatabase`.
sreallymatt marked this conversation as resolved.
Show resolved Hide resolved

## Attributes Reference
Expand Down
Loading