-
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.
- Loading branch information
1 parent
e343951
commit 0bb25f9
Showing
5 changed files
with
236 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "drone_repo Data Source - terraform-provider-drone" | ||
subcategory: "" | ||
description: |- | ||
Data source for retrieving a Drone repository | ||
--- | ||
|
||
# drone_repo (Data Source) | ||
|
||
Data source for retrieving a Drone repository | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `repository` (String) | ||
|
||
### Read-Only | ||
|
||
- `cancel_pulls` (Boolean) | ||
- `cancel_push` (Boolean) | ||
- `cancel_running` (Boolean) | ||
- `configuration` (String) | ||
- `id` (String) The ID of this resource. | ||
- `ignore_forks` (Boolean) | ||
- `ignore_pulls` (Boolean) | ||
- `protected` (Boolean) | ||
- `timeout` (Number) | ||
- `trusted` (Boolean) | ||
- `visibility` (String) | ||
|
||
|
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,23 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "drone_repos Data Source - terraform-provider-drone" | ||
subcategory: "" | ||
description: |- | ||
Data source for retrieving all repositories to which the user has explicit access in the host system | ||
--- | ||
|
||
# drone_repos (Data Source) | ||
|
||
Data source for retrieving all repositories to which the user has explicit access in the host system | ||
|
||
|
||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Read-Only | ||
|
||
- `id` (String) The ID of this resource. | ||
- `repositories` (List of String) | ||
|
||
|
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,114 @@ | ||
package drone | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"terraform-provider-drone/drone/utils" | ||
|
||
"github.com/drone/drone-go/drone" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func dataSourceRepo() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Data source for retrieving a Drone repository", | ||
ReadContext: dataSourceRepoRead, | ||
Schema: map[string]*schema.Schema{ | ||
"cancel_pulls": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"cancel_push": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"cancel_running": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"configuration": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"ignore_forks": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"ignore_pulls": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"protected": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"repository": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringMatch( | ||
regexp.MustCompile("^[^/ ]+/[^/ ]+$"), | ||
"Invalid repository (e.g. octocat/hello-world)", | ||
), | ||
}, | ||
"timeout": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"trusted": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"visibility": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceRepoRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(drone.Client) | ||
|
||
// Warning or errors can be collected in a slice type | ||
var diags diag.Diagnostics | ||
|
||
// Refresh repository list | ||
if _, err := client.RepoListSync(); err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
repository := d.Get("repository").(string) | ||
owner, name, err := utils.ParseRepo(repository) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
repo, err := client.Repo(owner, name) | ||
if err != nil { | ||
diags = append(diags, diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: fmt.Sprintf("Failed to read repo %s", repository), | ||
Detail: err.Error(), | ||
}) | ||
|
||
return diags | ||
} | ||
|
||
d.Set("cancel_pulls", repo.CancelPulls) | ||
d.Set("cancel_push", repo.CancelPush) | ||
d.Set("cancel_running", repo.CancelRunning) | ||
d.Set("configuration", repo.Config) | ||
d.Set("ignore_forks", repo.IgnoreForks) | ||
d.Set("ignore_pulls", repo.IgnorePulls) | ||
d.Set("protected", repo.Protected) | ||
d.Set("timeout", repo.Timeout) | ||
d.Set("trusted", repo.Trusted) | ||
d.Set("visibility", repo.Visibility) | ||
|
||
d.SetId(repository) | ||
|
||
return diags | ||
} |
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,61 @@ | ||
package drone | ||
|
||
import ( | ||
"context" | ||
"sort" | ||
|
||
"terraform-provider-drone/drone/utils" | ||
|
||
"github.com/drone/drone-go/drone" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceRepos() *schema.Resource { | ||
return &schema.Resource{ | ||
Description: "Data source for retrieving all repositories to which the user has explicit access in the host system", | ||
ReadContext: dataSourceReposRead, | ||
Schema: map[string]*schema.Schema{ | ||
"repositories": { | ||
Type: schema.TypeList, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceReposRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { | ||
client := m.(drone.Client) | ||
|
||
// Warning or errors can be collected in a slice type | ||
var diags diag.Diagnostics | ||
|
||
repos, err := client.RepoListSync() | ||
if err != nil { | ||
diags = append(diags, diag.Diagnostic{ | ||
Severity: diag.Error, | ||
Summary: "Failed to retrieve repositories", | ||
Detail: err.Error(), | ||
}) | ||
|
||
return diags | ||
} | ||
|
||
id := make([]string, 0) | ||
slugs := make([]string, 0) | ||
|
||
for _, repo := range repos { | ||
id = append(id, repo.Slug) | ||
slugs = append(slugs, repo.Slug) | ||
} | ||
|
||
sort.Strings(slugs) | ||
d.Set("repositories", slugs) | ||
|
||
d.SetId(utils.BuildChecksumID(id)) | ||
|
||
return diags | ||
} |
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