forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ashicorp#28456) * New resources: azurerm_mssql_job and azurerm_mssql_job_schedule * Update timeouts * Fix lint * Update struct name to follow convention * Apply suggestions from code review Co-authored-by: stephybun <[email protected]> * Update internal/services/mssql/mssql_job_schedule_resource.go Co-authored-by: stephybun <[email protected]> * Update based on comments --------- Co-authored-by: stephybun <[email protected]>
- Loading branch information
1 parent
113a570
commit 77da2eb
Showing
24 changed files
with
1,888 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
package mssql | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
"github.com/hashicorp/go-azure-helpers/lang/response" | ||
"github.com/hashicorp/go-azure-sdk/resource-manager/sql/2023-08-01-preview/jobs" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" | ||
) | ||
|
||
type MsSqlJobResource struct{} | ||
|
||
type MsSqlJobResourceModel struct { | ||
Name string `tfschema:"name"` | ||
JobAgentID string `tfschema:"job_agent_id"` | ||
Description string `tfschema:"description"` | ||
} | ||
|
||
var _ sdk.ResourceWithUpdate = MsSqlJobResource{} | ||
|
||
func (MsSqlJobResource) 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, | ||
ValidateFunc: jobs.ValidateJobAgentID, | ||
ForceNew: true, | ||
}, | ||
"description": { | ||
Type: pluginsdk.TypeString, | ||
Optional: true, | ||
}, | ||
} | ||
} | ||
|
||
func (MsSqlJobResource) Attributes() map[string]*pluginsdk.Schema { | ||
return map[string]*pluginsdk.Schema{} | ||
} | ||
|
||
func (MsSqlJobResource) ModelObject() interface{} { | ||
return &MsSqlJobResourceModel{} | ||
} | ||
|
||
func (MsSqlJobResource) ResourceType() string { | ||
return "azurerm_mssql_job" | ||
} | ||
|
||
func (r MsSqlJobResource) Create() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.MSSQL.JobsClient | ||
|
||
var model MsSqlJobResourceModel | ||
if err := metadata.Decode(&model); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
jobAgent, err := jobs.ParseJobAgentID(model.JobAgentID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
id := jobs.NewJobID(jobAgent.SubscriptionId, jobAgent.ResourceGroupName, jobAgent.ServerName, jobAgent.JobAgentName, model.Name) | ||
|
||
existing, err := client.Get(ctx, id) | ||
if err != nil && !response.WasNotFound(existing.HttpResponse) { | ||
return fmt.Errorf("checking for presence of existing %s: %+v", id, err) | ||
} | ||
|
||
if !response.WasNotFound(existing.HttpResponse) { | ||
return metadata.ResourceRequiresImport(r.ResourceType(), id) | ||
} | ||
|
||
parameters := jobs.Job{ | ||
Name: pointer.To(model.Name), | ||
Properties: pointer.To(jobs.JobProperties{ | ||
Description: pointer.To(model.Description), | ||
}), | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, id, parameters); err != nil { | ||
return fmt.Errorf("creating %s: %+v", id, err) | ||
} | ||
|
||
metadata.SetID(id) | ||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (MsSqlJobResource) Read() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 5 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.MSSQL.JobsClient | ||
|
||
id, err := jobs.ParseJobID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resp, err := client.Get(ctx, *id) | ||
if err != nil { | ||
if response.WasNotFound(resp.HttpResponse) { | ||
return metadata.MarkAsGone(id) | ||
} | ||
|
||
return fmt.Errorf("retrieving %s: %+v", id, err) | ||
} | ||
|
||
state := MsSqlJobResourceModel{ | ||
Name: id.JobName, | ||
JobAgentID: jobs.NewJobAgentID(id.SubscriptionId, id.ResourceGroupName, id.ServerName, id.JobAgentName).ID(), | ||
} | ||
|
||
if model := resp.Model; model != nil { | ||
if props := model.Properties; props != nil { | ||
state.Description = pointer.From(props.Description) | ||
} | ||
} | ||
|
||
return metadata.Encode(&state) | ||
}, | ||
} | ||
} | ||
|
||
func (MsSqlJobResource) Update() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.MSSQL.JobsClient | ||
|
||
id, err := jobs.ParseJobID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var config MsSqlJobResourceModel | ||
if err := metadata.Decode(&config); err != nil { | ||
return fmt.Errorf("decoding: %+v", err) | ||
} | ||
|
||
param := jobs.Job{ | ||
Properties: pointer.To(jobs.JobProperties{ | ||
Description: pointer.To(config.Description), | ||
}), | ||
} | ||
|
||
if _, err := client.CreateOrUpdate(ctx, *id, param); err != nil { | ||
return fmt.Errorf("updating %s: %+v", id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (MsSqlJobResource) Delete() sdk.ResourceFunc { | ||
return sdk.ResourceFunc{ | ||
Timeout: 30 * time.Minute, | ||
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
client := metadata.Client.MSSQL.JobsClient | ||
|
||
id, err := jobs.ParseJobID(metadata.ResourceData.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := client.Delete(ctx, *id); err != nil { | ||
return fmt.Errorf("deleting %s: %+v", id, err) | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
} | ||
|
||
func (MsSqlJobResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
return jobs.ValidateJobID | ||
} |
Oops, something went wrong.