Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
wuxu92 committed Sep 2, 2022
1 parent bc73af4 commit 455967c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 73 deletions.
62 changes: 35 additions & 27 deletions internal/services/automation/automation_source_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/Azure/azure-sdk-for-go/services/preview/automation/mgmt/2020-01-13-preview/automation"

"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/automation/validate"
Expand All @@ -16,24 +15,23 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

type SecurityToken struct {
type Security struct {
Token string `tfschema:"token"`
RefreshToken string `tfschema:"refresh_token"`
TokenType string `tfschema:"token_type"`
}

type SourceControlModel struct {
ResourceGroupName string `tfschema:"resource_group_name"`
AutomationAccountName string `tfschema:"automation_account_name"`
Name string `tfschema:"name"`
RepoURL string `tfschema:"repo_url"`
Branch string `tfschema:"branch"`
FolderPath string `tfschema:"folder_path"`
AutoSync bool `tfschema:"auto_sync"`
PublishRunbook bool `tfschema:"publish_runbook"`
SourceType string `tfschema:"source_type"`
Description string `tfschema:"description"`
SecurityToken []SecurityToken `tfschema:"security_token"`
AutomationAccountID string `tfschema:"automation_account_id"`
Name string `tfschema:"name"`
RepoURL string `tfschema:"repository_url"`
Branch string `tfschema:"branch"`
FolderPath string `tfschema:"folder_path"`
AutoSync bool `tfschema:"automatic_sync"`
PublishRunbook bool `tfschema:"publish_runbook_enabled"`
SourceType string `tfschema:"source_control_type"`
Description string `tfschema:"description"`
SecurityToken []Security `tfschema:"security"`
}

type SourceControlResource struct{}
Expand All @@ -42,45 +40,51 @@ var _ sdk.Resource = (*SourceControlResource)(nil)

func (m SourceControlResource) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
"resource_group_name": commonschema.ResourceGroupName(),
"automation_account_name": {
"automation_account_id": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.AutomationAccount(),
ValidateFunc: validate.AutomationAccountID,
},

"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"repo_url": {

"repository_url": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 2000),
},

"branch": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},

"folder_path": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},
"auto_sync": {

"automatic_sync": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},
"publish_runbook": {

"publish_runbook_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},
"source_type": {

"source_control_type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
Expand All @@ -89,11 +93,13 @@ func (m SourceControlResource) Arguments() map[string]*pluginsdk.Schema {
string(automation.SourceTypeGitHub),
}, true),
},

"description": {
Type: pluginsdk.TypeString,
Optional: true,
},
"security_token": {

"security": {
Type: pluginsdk.TypeList,
Required: true,
MaxItems: 1,
Expand All @@ -105,11 +111,13 @@ func (m SourceControlResource) Arguments() map[string]*pluginsdk.Schema {
Required: true,
ValidateFunc: validation.StringLenBetween(0, 1024),
},

"refresh_token": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 1024),
},

"token_type": {
Type: pluginsdk.TypeString,
Required: true,
Expand Down Expand Up @@ -148,7 +156,8 @@ func (m SourceControlResource) Create() sdk.ResourceFunc {
}

subscriptionID := meta.Client.Account.SubscriptionId
id := parse.NewSourceControlID(subscriptionID, model.ResourceGroupName, model.AutomationAccountName, model.Name)
accountID, _ := parse.AutomationAccountID(model.AutomationAccountID)
id := parse.NewSourceControlID(subscriptionID, accountID.ResourceGroup, accountID.Name, model.Name)
existing, err := client.Get(ctx, id.ResourceGroup, id.AutomationAccountName, id.Name)
if !utils.ResponseWasNotFound(existing.Response) {
if err != nil {
Expand Down Expand Up @@ -207,9 +216,8 @@ func (m SourceControlResource) Read() sdk.ResourceFunc {
if err := meta.Decode(&output); err != nil {
return err
}
output.AutomationAccountName = id.AutomationAccountName
output.AutomationAccountID = parse.NewAutomationAccountID(id.SubscriptionId, id.ResourceGroup, id.AutomationAccountName).ID()
output.Name = id.Name
output.ResourceGroupName = id.ResourceGroup
output.RepoURL = utils.NormalizeNilableString(result.RepoURL)
output.Branch = utils.NormalizeNilableString(result.Branch)
output.FolderPath = utils.NormalizeNilableString(result.FolderPath)
Expand Down Expand Up @@ -246,20 +254,20 @@ func (m SourceControlResource) Update() sdk.ResourceFunc {
if meta.ResourceData.HasChange("folder_path") {
prop.FolderPath = utils.String(model.FolderPath)
}
if meta.ResourceData.HasChange("auto_sync") {
if meta.ResourceData.HasChange("automatic_sync") {
prop.AutoSync = utils.Bool(model.AutoSync)
}
if meta.ResourceData.HasChange("folder_path") {
prop.FolderPath = utils.String(model.FolderPath)
}
if meta.ResourceData.HasChange("publish_runbook") {
if meta.ResourceData.HasChange("publish_runbook_enabled") {
prop.PublishRunbook = utils.Bool(model.PublishRunbook)
}
if meta.ResourceData.HasChange("description") {
prop.Description = utils.String(model.Description)
}

if meta.ResourceData.HasChange("security_token") {
if meta.ResourceData.HasChange("security") {
prop.SecurityToken = &automation.SourceControlSecurityTokenProperties{
AccessToken: utils.String(model.SecurityToken[0].TokenType),
RefreshToken: utils.String(model.SecurityToken[0].RefreshToken),
Expand Down
57 changes: 28 additions & 29 deletions internal/services/automation/automation_source_control_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ type githubRepo struct {
}

func newSourceControlResource(t *testing.T) SourceControlResource {
// - ARM_TEST_ASC_GITHUB_REPO_URL represents the user repo from: https://github.com/Azure-Samples/acr-build-helloworld-node
// - ARM_TEST_ASC_GITHUB_REPOSITORY_URL represents the user repo
// - ARM_TEST_ASC_GITHUB_USER_TOKEN represents the github personal token with the appropriate permissions per: https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tutorial-build-task#create-a-github-personal-access-token
// Checkout https://docs.microsoft.com/en-us/azure/container-registry/container-registry-tutorial-build-task for details.
ins := SourceControlResource{
githubRepo: githubRepo{
url: os.Getenv("ARM_TEST_ASC_GITHUB_REPO_URL"),
url: os.Getenv("ARM_TEST_ASC_GITHUB_REPOSITORY_URL"),
token: os.Getenv("ARM_TEST_ASC_GITHUB_USER_TOKEN"),
},
}
if ins.url == "" || ins.token == "" {
t.Skipf("both `ARM_TEST_ASC_GITHUB_REPO_URL` and `ARM_TEST_ASC_GITHUB_USER_TOKEN` must be set for acceptance tests!")
t.Skipf("both `ARM_TEST_ASC_GITHUB_REPOSITORY_URL` and `ARM_TEST_ASC_GITHUB_USER_TOKEN` must be set for acceptance tests!")
}
return ins
}
Expand Down Expand Up @@ -79,18 +79,18 @@ func (s SourceControlResource) basic(data acceptance.TestData) string {
%s
resource "azurerm_automation_source_control" "test" {
name = "acctest-%[2]d"
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
repo_url = "%[3]s"
branch = "main"
folder_path = "/runbook"
auto_sync = true
publish_runbook = true
source_type = "GitHub"
description = "example repo desc"
security_token {
name = "acctest-%[2]d"
automation_account_id = azurerm_automation_account.test.id
repository_url = "%[3]s"
branch = "main"
folder_path = "/runbook"
automatic_sync = true
publish_runbook_enabled = true
source_control_type = "GitHub"
description = "example repo desc"
security {
token = "%[4]s"
token_type = "PersonalAccessToken"
}
Expand All @@ -106,17 +106,16 @@ func (s SourceControlResource) update(data acceptance.TestData) string {
resource "azurerm_automation_source_control" "test" {
name = "acctest-%[2]d"
resource_group_name = azurerm_resource_group.test.name
automation_account_name = azurerm_automation_account.test.name
repo_url = "%[3]s"
branch = "dev"
folder_path = "/runbook"
auto_sync = true
publish_runbook = true
source_type = "GitHub"
description = "example repo desc foo"
security_token {
automation_account_id = azurerm_automation_account.test.id
repository_url = "%[3]s"
branch = "dev"
folder_path = "/runbook"
automatic_sync = true
publish_runbook_enabled = true
source_control_type = "GitHub"
description = "example repo desc foo"
security {
token = "%[4]s"
token_type = "PersonalAccessToken"
}
Expand All @@ -135,7 +134,7 @@ func TestAccSourceControl_basic(t *testing.T) {
check.That(data.ResourceName).Key("branch").HasValue("main"),
),
},
data.ImportStep("security_token"),
data.ImportStep("security"),
})
}

Expand All @@ -150,14 +149,14 @@ func TestAccSourceControl_update(t *testing.T) {
check.That(data.ResourceName).Key("branch").HasValue("main"),
),
},
data.ImportStep("security_token"),
data.ImportStep("security"),
{
Config: r.update(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("branch").HasValue("dev"),
),
},
data.ImportStep("security_token"),
data.ImportStep("security"),
})
}
32 changes: 15 additions & 17 deletions website/docs/r/automation_source_control.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ Manages an Automation Source Control.

```hcl
resource "azurerm_automation_source_control" "example" {
name = "example"
resource_group_name = "example"
automation_account_name = "example"
folder_path = "runbook"
name = "example"
resource_group_name = "example"
automation_account_id = azurerm_automation_account.test.id
folder_path = "runbook"
security_token {
security {
token = "ghp_xxx"
token_type = "PersonalAccessToken"
}
repo_url = "https://github.com/foo/bat.git"
source_type = "GitHub"
branch = "main"
repository_url = "https://github.com/foo/bat.git"
source_control_type = "GitHub"
branch = "main"
}
```

Expand All @@ -35,31 +35,29 @@ The following arguments are supported:

* `name` - (Required) The name which should be used for this Automation Source Control. Changing this forces a new Automation Source Control to be created.

* `automation_account_name` - (Required) The name of the automation account in which the Runbook is created. Changing this forces a new resource to be created.

* `resource_group_name` - (Required) The name of the resource group in which the Runbook is created. Changing this forces a new resource to be created.
* `automation_account_id` (Required) The ID of Automation Account to manage this Source Control. Changing this forces a new Automation Source Control to be created.

* `folder_path` - (Required) The folder path of the source control. This Path must be relative.

* `repo_url` - (Required) The Repo URL of the source control.
* `repository_url` - (Required) The Repository URL of the source control.

* `security_token` - (Required) A `security_token` block as defined below.
* `security` - (Required) A `security` block as defined below.

* `source_type` - (Required) The source type of Source Control, possible vaules are `VsoGit`, `VsoTfvc` and `GitHub`, and the value is case sensitive.
* `source_control_type` - (Required) The source type of Source Control, possible vaules are `VsoGit`, `VsoTfvc` and `GitHub`, and the value is case sensitive.

---

* `auto_sync` - (Optional) Whether auto async the Source Control.
* `automatic_sync` - (Optional) Whether auto async the Source Control.

* `branch` - (Optional) Specify the repo branch of the Source Control. Empty value is valid only for `VsoTfvc`.

* `description` - (Optional) A short description of the Source Control.

* `publish_runbook` - (Optional) Whether auto publish the Source Control.
* `publish_runbook_enabled` - (Optional) Whether auto publish the Source Control.

---

A `security_token` block supports the following:
A `security` block supports the following:

* `token` - (Required) The access token of specified repo.

Expand Down

0 comments on commit 455967c

Please sign in to comment.