Skip to content

Commit

Permalink
repo and repos data sources
Browse files Browse the repository at this point in the history
  • Loading branch information
jimsheldon committed Jul 7, 2022
1 parent e343951 commit 0bb25f9
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 0 deletions.
36 changes: 36 additions & 0 deletions docs/data-sources/repo.md
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)


23 changes: 23 additions & 0 deletions docs/data-sources/repos.md
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)


114 changes: 114 additions & 0 deletions drone/data_source_repo.go
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
}
61 changes: 61 additions & 0 deletions drone/data_source_repos.go
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
}
2 changes: 2 additions & 0 deletions drone/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func Provider() *schema.Provider {
"drone_user": resourceUser(),
},
DataSourcesMap: map[string]*schema.Resource{
"drone_repo": dataSourceRepo(),
"drone_repos": dataSourceRepos(),
"drone_template": dataSourceTemplate(),
"drone_templates": dataSourceTemplates(),
"drone_user": dataSourceUser(),
Expand Down

0 comments on commit 0bb25f9

Please sign in to comment.