This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test build 2.4.0 with vulnerability alerts
- Loading branch information
Showing
6 changed files
with
289 additions
and
27 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
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,95 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
) | ||
|
||
func resourceGithubRepositoryVulnerabilityAlerts() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceGithubRepositoryVulnerabilityAlertsCreate, | ||
Read: resourceGithubRepositoryVulnerabilityAlertsRead, | ||
Delete: resourceGithubRepositoryVulnerabilityAlertsDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceGithubRepositoryVulnerabilityAlertsCreate(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := meta.(*Organization).client | ||
|
||
orgName := meta.(*Organization).name | ||
repoName := d.Get("repository").(string) | ||
|
||
log.Printf("[DEBUG] Creating repository vulnerability alerts: %s/%s", orgName, repoName) | ||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
_, err = client.Repositories.EnableVulnerabilityAlerts(ctx, orgName, repoName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(repoName) | ||
|
||
return resourceGithubRepositoryVulnerabilityAlertsRead(d, meta) | ||
} | ||
|
||
func resourceGithubRepositoryVulnerabilityAlertsRead(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := meta.(*Organization).client | ||
|
||
repoName := d.Id() | ||
if err != nil { | ||
return err | ||
} | ||
orgName := meta.(*Organization).name | ||
|
||
log.Printf("[DEBUG] Reading repository vulnerability alerts: %s/%s", orgName, repoName) | ||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
enabled, _, err := client.Repositories.GetVulnerabilityAlerts(ctx, orgName, repoName) | ||
if err != nil { | ||
return fmt.Errorf("Error reading repository vulnerability alerts: %v", err) | ||
} | ||
if !enabled { | ||
d.SetId("") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceGithubRepositoryVulnerabilityAlertsDelete(d *schema.ResourceData, meta interface{}) error { | ||
err := checkOrganization(meta) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
client := meta.(*Organization).client | ||
repoName := d.Id() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
orgName := meta.(*Organization).name | ||
ctx := context.WithValue(context.Background(), ctxId, d.Id()) | ||
log.Printf("[DEBUG] Deleting repository vulnerability alerts%s/%s", orgName, repoName) | ||
_, err = client.Repositories.DisableVulnerabilityAlerts(ctx, orgName, repoName) | ||
|
||
return err | ||
} |
94 changes: 94 additions & 0 deletions
94
github/resource_github_repository_vulnerability_alerts_test.go
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,94 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/terraform" | ||
) | ||
|
||
func TestAccGithubRepositoryVulnerabilityAlerts_basic(t *testing.T) { | ||
rn := "github_repository_vulnerability_alerts.test" | ||
rString := acctest.RandString(5) | ||
repoName := fmt.Sprintf("tf-acc-test-repository-vulnerability-alerts-%s", rString) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccGithubRepositoryVulnerabilityAlertsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccGithubRepositoryVulnerabilityAlertsConfig(repoName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckGithubRepositoryVulnerabilityAlertsExists(rn, repoName), | ||
resource.TestCheckResourceAttr(rn, "repository", repoName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGithubRepositoryVulnerabilityAlertsExists(n, id string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not Found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID != id { | ||
return fmt.Errorf("Expected ID to be %v, got %v", id, rs.Primary.ID) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*Organization).client | ||
o := testAccProvider.Meta().(*Organization).name | ||
|
||
enabled, _, err := conn.Repositories.GetVulnerabilityAlerts(context.TODO(), o, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !enabled { | ||
return fmt.Errorf("Expected vulnerability alerts to be enabled on %v", id) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccGithubRepositoryVulnerabilityAlertsDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*Organization).client | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "github_repository_vulnerability_alerts" { | ||
continue | ||
} | ||
|
||
o := testAccProvider.Meta().(*Organization).name | ||
enabled, _, err := conn.Repositories.GetVulnerabilityAlerts(context.TODO(), o, rs.Primary.ID) | ||
|
||
if err == nil { | ||
if enabled { | ||
return fmt.Errorf("Repositor vulnerability alerts still exist") | ||
} | ||
} | ||
return nil | ||
} | ||
return nil | ||
} | ||
|
||
func testAccGithubRepositoryVulnerabilityAlertsConfig(repoName string) string { | ||
return fmt.Sprintf(` | ||
resource "github_repository" "test" { | ||
name = "%s" | ||
description = "Terraform Acceptance Test %s" | ||
auto_init = true | ||
} | ||
resource "github_repository_vulnerability_alerts" "test" { | ||
repository = "${github_repository.test.name}" | ||
} | ||
`, repoName, repoName) | ||
} |
47 changes: 47 additions & 0 deletions
47
website/docs/r/repository_vulnerability_alerts.html.markdown
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,47 @@ | ||
--- | ||
layout: "github" | ||
page_title: "GitHub: github_repository_vulnerability_alerts" | ||
description: |- | ||
Provides a resource for vulnerability alerts on Github repositories. | ||
--- | ||
|
||
# github_repository_vulnerability_alerts | ||
|
||
Provides a GitHub repository vulnerability alerts resource. | ||
|
||
This resource allows you to enable/disable vulnerability alerts on a repository in your organization. | ||
|
||
Check the documentation below for preconditions a repository needs to fulfill for having vulenerability alerts. | ||
|
||
Further documentation on GitHub vulnerability alerts can be found here: | ||
|
||
- [About automated security updates](https://help.github.com/en/github/managing-security-vulnerabilities/configuring-automated-security-updates#about-automated-security-updates) | ||
- [Enable or disable vulnerability alerts for a repository by API](https://developer.github.com/changes/2019-04-24-vulnerability-alerts/) | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
# Enable vulenerability alerts on a repository | ||
resource "github_repository" "foo" { | ||
name = "foo" | ||
auto_init = true | ||
} | ||
resource "github_repository_vulnerability_alerts" "foo" { | ||
repository = "${github_repository.foo.name}" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `repository` - (Required) The GitHub repository | ||
|
||
## Attribute Reference | ||
|
||
In addition to the above arguments there no further attributes. | ||
|
||
## Import | ||
|
||
Importing this resource is currently not supported. |
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