Skip to content

Commit

Permalink
addd runbook worker resource
Browse files Browse the repository at this point in the history
update worker resource
  • Loading branch information
wuxu92 committed Jul 12, 2022
1 parent ba10991 commit 6bbdfd8
Show file tree
Hide file tree
Showing 24 changed files with 1,488 additions and 8 deletions.
199 changes: 199 additions & 0 deletions internal/services/automation/automation_hybrid_runbook_worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package automation

import (
"context"
"fmt"
"net/http"
"time"

"github.com/hashicorp/terraform-provider-azurerm/utils"

"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2021-06-22/hybridrunbookworker"
"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 HybridRunbookWorkerModel struct {
ResourceGroupName string `tfschema:"resource_group_name"`
AutomationAccountName string `tfschema:"automation_account_name"`
WorkerGroupName string `tfschema:"worker_group_name"`
WorkerName string `tfschema:"worker_name"`
WorkerId string `tfschema:"worker_id"`
VmResourceId string `tfschema:"vm_resource_id"`
Ip string `tfschema:"ip"`
RegisteredDateTime string `tfschema:"registered_date_time"`
LastSeenDateTime string `tfschema:"last_seen_date_time"`
WorkerType string `tfschema:"worker_type"`
}

type HybridRunbookWorkerResource struct{}

var _ sdk.Resource = (*HybridRunbookWorkerResource)(nil)

func (m HybridRunbookWorkerResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"resource_group_name": commonschema.ResourceGroupName(),
"automation_account_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"worker_group_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"worker_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
"vm_resource_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
}
}

func (m HybridRunbookWorkerResource) Attributes() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"ip": {
Type: pluginsdk.TypeString,
Computed: true,
},
"registered_date_time": {
Type: pluginsdk.TypeString,
Computed: true,
},
"last_seen_date_time": {
Type: pluginsdk.TypeString,
Computed: true,
},
"worker_name": {
Type: pluginsdk.TypeString,
Computed: true,
},
"worker_type": {
Type: pluginsdk.TypeString,
Computed: true,
},
}
}

func (m HybridRunbookWorkerResource) ModelObject() interface{} {
return &HybridRunbookWorkerModel{}
}

func (m HybridRunbookWorkerResource) ResourceType() string {
return "azurerm_automation_hybrid_runbook_worker"
}

func (m HybridRunbookWorkerResource) Create() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 30 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
client := meta.Client.Automation.RunbookWorkerClient

var model HybridRunbookWorkerModel
if err := meta.Decode(&model); err != nil {
return err
}

subscriptionID := meta.Client.Account.SubscriptionId
id := hybridrunbookworker.NewHybridRunbookWorkerID(subscriptionID, model.ResourceGroupName,
model.AutomationAccountName, model.WorkerGroupName, model.WorkerId)
existing, err := client.Get(ctx, id)
if !response.WasNotFound(existing.HttpResponse) {
if err != nil {
return fmt.Errorf("retreiving %s: %v", id, err)
}
return meta.ResourceRequiresImport(m.ResourceType(), id)
}

req := hybridrunbookworker.HybridRunbookWorkerCreateParameters{}
if model.VmResourceId != "" {
req.Properties.VmResourceId = utils.String(model.VmResourceId)
}

future, err := client.Create(ctx, id, req)
if err != nil {
// Workaround swagger issue https://github.com/Azure/azure-rest-api-specs/issues/19741
if !response.WasStatusCode(future.HttpResponse, http.StatusCreated) {
return fmt.Errorf("creating %s: %v", id, err)
}
}
_ = future

meta.SetID(id)
return nil
},
}
}

func (m HybridRunbookWorkerResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 5 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
id, err := hybridrunbookworker.ParseHybridRunbookWorkerID(meta.ResourceData.Id())
if err != nil {
return err
}
client := meta.Client.Automation.RunbookWorkerClient
result, err := client.Get(ctx, *id)
if err != nil {
return err
}
if result.Model == nil {
return fmt.Errorf("retrieving %s got nil model", id)
}

var output HybridRunbookWorkerModel

// the name in response corresponding to work_id in request
output.WorkerId = utils.NormalizeNilableString(result.Model.Name)
output.AutomationAccountName = id.AutomationAccountName
output.ResourceGroupName = id.ResourceGroupName
output.WorkerGroupName = id.HybridRunbookWorkerGroupName
if prop := result.Model.Properties; prop != nil {
output.VmResourceId = utils.NormalizeNilableString(prop.VmResourceId)
output.WorkerType = utils.NormalizeNilableString((*string)(prop.WorkerType))
output.LastSeenDateTime = utils.NormalizeNilableString(prop.LastSeenDateTime)
output.RegisteredDateTime = utils.NormalizeNilableString(prop.RegisteredDateTime)
output.Ip = utils.NormalizeNilableString(prop.Ip)
output.WorkerName = utils.NormalizeNilableString(prop.WorkerName)
}
// TODO: populate other fields
return meta.Encode(&output)
},
}
}

func (m HybridRunbookWorkerResource) Delete() sdk.ResourceFunc {
return sdk.ResourceFunc{
Timeout: 10 * time.Minute,
Func: func(ctx context.Context, meta sdk.ResourceMetaData) error {
id, err := hybridrunbookworker.ParseHybridRunbookWorkerID(meta.ResourceData.Id())
if err != nil {
return err
}
meta.Logger.Infof("deleting %s", id)
client := meta.Client.Automation.RunbookWorkerClient
if _, err = client.Delete(ctx, *id); err != nil {
return fmt.Errorf("deleting %s: %v", id, err)
}
return nil
},
}
}

func (m HybridRunbookWorkerResource) IDValidationFunc() pluginsdk.SchemaValidateFunc {
return hybridrunbookworker.ValidateHybridRunbookWorkerID
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package automation
import (
"context"
"fmt"
"net/http"
"time"

"github.com/hashicorp/go-azure-helpers/lang/response"
Expand Down Expand Up @@ -31,11 +32,13 @@ func (m HybridRunbookWorkerGroupResource) Arguments() map[string]*pluginsdk.Sche
"automation_account_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"credential_name": {
Expand Down Expand Up @@ -87,7 +90,10 @@ func (m HybridRunbookWorkerGroupResource) Create() sdk.ResourceFunc {
}
future, err := client.Create(ctx, id, req)
if err != nil {
return fmt.Errorf("creating %s: %v", id, err)
// Workaround swagger issue https://github.com/Azure/azure-rest-api-specs/issues/19741
if !response.WasStatusCode(future.HttpResponse, http.StatusCreated) {
return fmt.Errorf("creating %s: %v", id, err)
}
}
_ = future

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func (a HybridRunbookWorkerGroupResource) basic(data acceptance.TestData) string
%s
resource "azurerm_automation_hybrid_runbook_worker_group" "test" {
// TODO modify fields
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
name = "acctest-%[2]d"
Expand All @@ -87,8 +85,7 @@ resource "azurerm_automation_credential" "test2" {
password = "test_pwd"
}
resource "azurerm_automation_connection_type" "test" {
resource "azurerm_automation_hybrid_runbook_worker_group" "test" {
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
name = "acctest-%[2]d"
Expand All @@ -105,7 +102,6 @@ func TestAccHybridRunbookWorkerGroup_basic(t *testing.T) {
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("credential_name").HasValue("example-foo"),
),
},
})
Expand All @@ -119,15 +115,13 @@ func TestAccHybridRunbookWorkerGroup_update(t *testing.T) {
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("credential_name").HasValue("example-foo"),
),
},
data.ImportStep(),
{
Config: r.update(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("credential_name").HasValue("example-bar"),
),
},
data.ImportStep(),
Expand Down
Loading

0 comments on commit 6bbdfd8

Please sign in to comment.