-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: fetch Github Installation Id from env0 (#934)
* Feat: fetch Github Installation Id from env0 * modify test * modify test * formatting * add example * fix test * fix typo
- Loading branch information
1 parent
6e3153f
commit 322bbc6
Showing
12 changed files
with
386 additions
and
177 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,46 @@ | ||
package client_test | ||
|
||
import ( | ||
. "github.com/env0/terraform-provider-env0/client" | ||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
var _ = Describe("VCSToken", func() { | ||
vcsType := "github" | ||
repository := "http://myrepo.com/" | ||
|
||
mockVcsToken := VscToken{ | ||
Token: 12345, | ||
} | ||
|
||
Describe("get", func() { | ||
var ret *VscToken | ||
var err error | ||
|
||
BeforeEach(func() { | ||
mockOrganizationIdCall(organizationId) | ||
|
||
httpCall = mockHttpClient.EXPECT(). | ||
Get("/vcs-token/"+vcsType, map[string]string{ | ||
"organizationId": organizationId, | ||
"repository": repository, | ||
}, gomock.Any()). | ||
Do(func(path string, request interface{}, response *VscToken) { | ||
*response = mockVcsToken | ||
}).Times(1) | ||
|
||
ret, err = apiClient.VcsToken(vcsType, repository) | ||
}) | ||
|
||
It("should return vcs token", func() { | ||
Expect(*ret).To(Equal(mockVcsToken)) | ||
}) | ||
|
||
It("should not return error", func() { | ||
Expect(err).To(BeNil()) | ||
}) | ||
}) | ||
|
||
}) |
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,22 @@ | ||
package client | ||
|
||
type VscToken struct { | ||
Token int `json:"token"` | ||
} | ||
|
||
func (client *ApiClient) VcsToken(vcsType string, repository string) (*VscToken, error) { | ||
organizationId, err := client.OrganizationId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var result VscToken | ||
if err := client.http.Get("/vcs-token/"+vcsType, map[string]string{ | ||
"organizationId": organizationId, | ||
"repository": repository, | ||
}, &result); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &result, nil | ||
} |
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,46 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataGithubInstallationId() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "returns the github installation id of a git hub repositroy", | ||
|
||
ReadContext: dataGithubInstallationIdRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Description: "the name of the repository", | ||
Required: true, | ||
}, | ||
"github_installation_id": { | ||
Type: schema.TypeInt, | ||
Description: "the github installation id", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataGithubInstallationIdRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
repositroy := d.Get("repository").(string) | ||
|
||
token, err := apiClient.VcsToken("github", repositroy) | ||
if err != nil { | ||
return diag.Errorf("failed to get github installation id: %v", err) | ||
} | ||
|
||
d.Set("github_installation_id", token.Token) | ||
d.SetId(repositroy) | ||
|
||
return nil | ||
} |
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,69 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestGithubInstallationIdDataSource(t *testing.T) { | ||
mockToken := client.VscToken{ | ||
Token: 12345, | ||
} | ||
|
||
mockRepositroy := "http://myrepo.com" | ||
|
||
resourceType := "env0_github_installation_id" | ||
resourceName := "test" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getValidTestCase := func(repository string) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"repository": repository, | ||
}), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "github_installation_id", strconv.Itoa(mockToken.Token)), | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
getErrorTestCase := func(repository string, expectedError string) resource.TestCase { | ||
return resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{ | ||
"repository": repository, | ||
}), | ||
ExpectError: regexp.MustCompile(expectedError), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
t.Run("get by repository", func(t *testing.T) { | ||
runUnitTest(t, | ||
getValidTestCase(mockRepositroy), | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().VcsToken("github", mockRepositroy).Return(&mockToken, nil).AnyTimes() | ||
}, | ||
) | ||
}) | ||
|
||
t.Run("get by repository - failed", func(t *testing.T) { | ||
runUnitTest(t, | ||
getErrorTestCase(mockRepositroy, "failed to get github installation id: error"), | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().VcsToken("github", mockRepositroy).Return(nil, errors.New("error")).AnyTimes() | ||
}, | ||
) | ||
}) | ||
} |
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
7 changes: 7 additions & 0 deletions
7
examples/data-sources/env0_github_installation_id/data-source.tf
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,7 @@ | ||
data "env0_github_installation_id" "example" { | ||
repository = "https://github.com/env0/templates" | ||
} | ||
|
||
output "github_installation_id" { | ||
value = data.env0_github_installation_id.example.github_installation_id | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
{ | ||
"github_template_type": "terraform", | ||
"github_template_name": "Github Test-", | ||
"github_template_repository": "https://github.com/env0/templates", | ||
"gitlab_template_repository": "https://gitlab.com/env0/gitlab-vcs-integration-tests.git", | ||
"github_template_path": "/second", | ||
"tg_tg_version" : "0.35.0", | ||
"data_github_template_type": "terraform", | ||
"github_variables_name": "email", | ||
"github_variables_value": "[email protected]" | ||
} | ||
{ | ||
"github_template_type": "terraform", | ||
"github_template_name": "Github Test-", | ||
"github_template_repository": "https://github.com/env0/templates", | ||
"gitlab_template_repository": "https://gitlab.com/env0/gitlab-vcs-integration-tests.git", | ||
"github_template_path": "/second", | ||
"tg_tg_version": "0.35.0", | ||
"data_github_template_type": "terraform", | ||
"github_variables_name": "email", | ||
"github_variables_value": "[email protected]" | ||
} |
Oops, something went wrong.