Skip to content

Commit

Permalink
Add jenkins_plugins data resource (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
wengchaoxi committed Aug 10, 2022
1 parent e5f661b commit f3f44e1
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 1 deletion.
20 changes: 20 additions & 0 deletions docs/data-sources/plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# jenkins_plugin Data Source

Get the information of plugins within Jenkins.

## Example Usage

```hcl
data "jenkins_plugins" "example" {}
```

## Argument Reference

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - Fixed value: `jenkins-data-source-plugins-id`.
* `plugins` - The list of the Jenkins plugins.
* `name` - The name of plugin.
* `version` - The version of plugin.
2 changes: 1 addition & 1 deletion example/providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ terraform {
required_providers {
jenkins = {
source = "kingsoftgames/jenkins"
version = ">= 0.5.0"
version = ">= 0.0.1"
}
}
}
57 changes: 57 additions & 0 deletions jenkins/data_source_jenkins_plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package jenkins

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceJenkinsPlugins() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePluginsRead,
Schema: map[string]*schema.Schema{
"plugins": {
Type: schema.TypeList,
Description: "The list of the Jenkins plugins.",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourcePluginsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*jenkinsAdapter)

p, err := client.GetPlugins(ctx, 1)
if err != nil {
return diag.FromErr(err)
}

var plugins []map[string]string
for _, v := range p.Raw.Plugins {
p := make(map[string]string)
p["name"] = v.ShortName
p["version"] = v.Version
plugins = append(plugins, p)
}

if err := d.Set("plugins", plugins); err != nil {
return diag.FromErr(err)
}

d.SetId("jenkins-data-source-plugins-id")
return nil
}
22 changes: 22 additions & 0 deletions jenkins/data_source_jenkins_plugins_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package jenkins

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccJenkinsPluginsDataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: "data jenkins_plugins foo {}",
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.jenkins_plugins.foo", "id", "jenkins-data-source-plugins-id"),
),
},
},
})
}
1 change: 1 addition & 0 deletions jenkins/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Provider() *schema.Provider {
"jenkins_credential_vault_approle": dataSourceJenkinsCredentialVaultAppRole(),
"jenkins_folder": dataSourceJenkinsFolder(),
"jenkins_job": dataSourceJenkinsJob(),
"jenkins_plugins": dataSourceJenkinsPlugins(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down

0 comments on commit f3f44e1

Please sign in to comment.