Skip to content

Commit

Permalink
Merge pull request #104 from Scalr/fix/SCALRCORE-20952
Browse files Browse the repository at this point in the history
SCALRCORE-20952 API > Create VCS provider with personal token > Provider is broken
  • Loading branch information
penja committed Feb 7, 2022
2 parents f89b500 + 8fcb613 commit d4744c7
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed
- create vcs_provider with bitbucket_enterprise vcs_type ([#104](https://github.com/Scalr/terraform-provider-scalr/pull/104))

### Required

- scalr-server >= `8.10.0`

## [1.0.0-rc26] - 2022-01-21

### Changed
- **New resource:** `scalr_run_triggers` ([#102](https://github.com/Scalr/terraform-provider-scalr/pull/102))
- `data.scalr_environment`: allow obtaining scalr_environment by name ([#101](https://github.com/Scalr/terraform-provider-scalr/pull/101))
- `data.scalr_environment`: allow to obtain scalr_environment by name ([#101](https://github.com/Scalr/terraform-provider-scalr/pull/101))
- `data.scalr_environment`: `id` become optional ([#101](https://github.com/Scalr/terraform-provider-scalr/pull/101))
- `data.scalr_environment`: added new optional attribute `name` ([#101](https://github.com/Scalr/terraform-provider-scalr/pull/101))
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/scalr_vcs_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ resource "scalr_vcs_provider" "example" {

* `name` - (Required) Name of the vcs provider.
* `vcs_type` (Required) The vcs provider type is one of `github`, `github_enterprise`, `gitlab`, `gitlab_enterprise`, `bitbucket_enterprise`.
The other providers currently is not supported in resource.
The other providers currently are not supported in resource.
* `token` (Required) The personal access token for provider
* GitHub token can be generated by url https://github.com/settings/tokens/new?description=example-vcs-resouce&scopes=repo
* Gitlab token can be generated by url https://gitlab.com/-/profile/personal_access_tokens?name=example-vcs-resouce&scopes=api,read_user,read_registry
* `account_id` - (Optional) ID of the account.
* `url` - (Optional) This field is required for self-hosted vcs providers.
* `username` - (Optional) This field is required for `bitbucket_enterprise` provider type.


## Attribute Reference
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/hashicorp/terraform-plugin-sdk v1.17.2
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734
github.com/scalr/go-scalr v0.0.0-20211223122739-8bea92373af4
github.com/scalr/go-scalr v0.0.0-20220117150518-eab908c663b7
)

go 1.13
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ github.com/posener/complete v1.2.1 h1:LrvDIY//XNo65Lq84G/akBuMGlawHvGBABv8f/ZN6D
github.com/posener/complete v1.2.1/go.mod h1:6gapUrK/U1TAN7ciCoNRIdVC5sbdBTUh1DKN0g6uH7E=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/scalr/go-scalr v0.0.0-20211223122739-8bea92373af4 h1:rVIhSDczLrSyE3snV9Og5yIz2xxP5Bow5O1MQl8B+B0=
github.com/scalr/go-scalr v0.0.0-20211223122739-8bea92373af4/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/scalr/go-scalr v0.0.0-20220117150518-eab908c663b7 h1:7vrzncW+AcglYgFleoIDi1+eBfPVuvREYKS37Emio1I=
github.com/scalr/go-scalr v0.0.0-20220117150518-eab908c663b7/go.mod h1:n2HQ6QxqyTySiSFTOpAL18SjCKtP+xUskMygO0KuuQU=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
23 changes: 23 additions & 0 deletions scalr/resource_scalr_vcs_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func resourceScalrVcsProvider() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceScalrVcsProviderV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceScalrVcsProviderStateUpgradeV0,
Version: 0,
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -49,6 +57,10 @@ func resourceScalrVcsProvider() *schema.Resource {
Required: true,
Sensitive: true,
},
"username": {
Type: schema.TypeString,
Optional: true,
},
"account_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -77,6 +89,11 @@ func resourceScalrVcsProviderCreate(d *schema.ResourceData, meta interface{}) er
options.Url = scalr.String(url.(string))
}

// Get the username
if username, ok := d.GetOk("username"); ok {
options.Username = scalr.String(username.(string))
}

// Get the account
if accountId, ok := d.GetOk("account_id"); ok {
options.Account = &scalr.Account{
Expand Down Expand Up @@ -107,6 +124,7 @@ func resourceScalrVcsProviderRead(d *schema.ResourceData, meta interface{}) erro
d.Set("url", provider.Url)
d.Set("vcs_type", provider.VcsType)
d.Set("auth_type", provider.AuthType)
d.Set("username", provider.Username)
if provider.Account != nil {
d.Set("account_id", provider.Account.ID)
}
Expand All @@ -126,6 +144,11 @@ func resourceScalrVcsProviderUpdate(d *schema.ResourceData, meta interface{}) er
options.Url = scalr.String(url.(string))
}

// Get the username
if username, ok := d.GetOk("username"); ok {
options.Username = scalr.String(username.(string))
}

log.Printf("[DEBUG] Update vcs provider: %s", d.Id())
_, err := scalrClient.VcsProviders.Update(ctx, d.Id(), options)
if err != nil {
Expand Down
54 changes: 54 additions & 0 deletions scalr/resource_scalr_vcs_provider_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package scalr

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/scalr/go-scalr"
)

func resourceScalrVcsProviderV0() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"url": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"vcs_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(
[]string{
string(scalr.Github),
string(scalr.GithubEnterprise),
string(scalr.Gitlab),
string(scalr.GitlabEnterprise),
string(scalr.BitbucketEnterprise),
},
false,
),
},
"token": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"account_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}

func resourceScalrVcsProviderStateUpgradeV0(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
rawState["username"] = ""
return rawState, nil
}
27 changes: 27 additions & 0 deletions scalr/resource_scalr_vcs_provider_migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scalr

import (
"testing"
)

func testResourceScalrVcsProviderStateDataV0() map[string]interface{} {
return map[string]interface{}{
"id": "vcs-123",
"name": "test",
"token": "tolen",
"url": "https://github.com",
"vcs_type": "github",
}
}

func testResourceScalrVcsProviderStateDataV1() map[string]interface{} {
res := testResourceScalrVcsProviderStateDataV0()
res["username"] = ""
return res
}

func TestResourceScalrVcsProviderStateUpgradeV0(t *testing.T) {
expected := testResourceScalrVcsProviderStateDataV1()
actual, err := resourceScalrVcsProviderStateUpgradeV0(testResourceScalrVcsProviderStateDataV0(), nil)
assertCorrectState(t, err, actual, expected)
}

0 comments on commit d4744c7

Please sign in to comment.