Skip to content

Commit

Permalink
addd runbook worker resource
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 committed Aug 30, 2022
1 parent 1163a9e commit ef09365
Show file tree
Hide file tree
Showing 24 changed files with 1,487 additions and 0 deletions.
198 changes: 198 additions & 0 deletions internal/services/automation/automation_hybrid_runbook_worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
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)
}
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 @@ -32,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 @@ -88,6 +90,7 @@ func (m HybridRunbookWorkerGroupResource) Create() sdk.ResourceFunc {
}
// return 201 cause err in autorest sdk
future, err := client.Create(ctx, id, req)
// Workaround swagger issue https://github.com/Azure/azure-rest-api-specs/issues/19741
if err != nil && !response.WasStatusCode(future.HttpResponse, http.StatusCreated) {
return fmt.Errorf("creating %s: %v", id, err)
}
Expand Down
153 changes: 153 additions & 0 deletions internal/services/automation/automation_hybrid_runbook_worker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package automation_test

import (
"context"
"fmt"
"testing"

"github.com/google/uuid"

"github.com/hashicorp/go-azure-sdk/resource-manager/automation/2021-06-22/hybridrunbookworker"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type HybridRunbookWorkerResource struct{}

func (a HybridRunbookWorkerResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := hybridrunbookworker.ParseHybridRunbookWorkerID(state.ID)
if err != nil {
return nil, err
}
resp, err := client.Automation.RunbookWorkerClient.Get(ctx, *id)
if err != nil {
return nil, fmt.Errorf("retrieving HybridRunbookWorker %s: %+v", id, err)
}
return utils.Bool(resp.Model != nil), nil
}

func (a HybridRunbookWorkerResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-auto-%[1]d"
location = "%[2]s"
}
resource "azurerm_automation_account" "test" {
name = "acctestAA-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
sku_name = "Basic"
}
resource "azurerm_automation_credential" "test" {
name = "acctest-%[1]d"
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
username = "test_user"
password = "test_pwd"
}
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-%[1]d"
credential_name = azurerm_automation_credential.test.name
}
resource "azurerm_virtual_network" "test" {
name = "acctestnw-%[1]d"
address_space = ["10.0.0.0/16"]
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
}
resource "azurerm_subnet" "test" {
name = "internal"
resource_group_name = azurerm_resource_group.test.name
virtual_network_name = azurerm_virtual_network.test.name
address_prefixes = ["10.0.2.0/24"]
}
resource "azurerm_network_interface" "test" {
name = "acctni-%[1]d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
ip_configuration {
name = "testconfiguration1"
subnet_id = azurerm_subnet.test.id
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_linux_virtual_machine" "test" {
name = "acctestVM-%[1]d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
size = "Standard_D2s_v3"
admin_username = "adminuser"
admin_password = "P@$$w0rd1234!"
disable_password_authentication = false
network_interface_ids = [
azurerm_network_interface.test.id,
]
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
tags = {
azsecpack = "nonprod"
"platformsettings.host_environment.service.platform_optedin_for_rootcerts" = "true"
}
}
`, data.RandomInteger, data.Locations.Primary)
}

func (a HybridRunbookWorkerResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s
resource "azurerm_automation_hybrid_runbook_worker" "test" {
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
worker_group_name = azurerm_automation_hybrid_runbook_worker_group.test.name
worker_id = "%s"
vm_resource_id = azurerm_linux_virtual_machine.test.id
}
`, a.template(data), uuid.New().String())
}

func TestAccHybridRunbookWorker_basic(t *testing.T) {
data := acceptance.BuildTestData(t, automation.HybridRunbookWorkerResource{}.ResourceType(), "test")
r := HybridRunbookWorkerResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
})
}
Loading

0 comments on commit ef09365

Please sign in to comment.