diff --git a/docs/data-sources/plugins.md b/docs/data-sources/plugins.md new file mode 100644 index 0000000..01c24e7 --- /dev/null +++ b/docs/data-sources/plugins.md @@ -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. diff --git a/example/providers.tf b/example/providers.tf index b6a71f5..3445278 100644 --- a/example/providers.tf +++ b/example/providers.tf @@ -11,7 +11,7 @@ terraform { required_providers { jenkins = { source = "kingsoftgames/jenkins" - version = ">= 0.5.0" + version = ">= 0.0.1" } } } diff --git a/jenkins/data_source_jenkins_plugins.go b/jenkins/data_source_jenkins_plugins.go new file mode 100644 index 0000000..c178146 --- /dev/null +++ b/jenkins/data_source_jenkins_plugins.go @@ -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 +} diff --git a/jenkins/data_source_jenkins_plugins_test.go b/jenkins/data_source_jenkins_plugins_test.go new file mode 100644 index 0000000..d888995 --- /dev/null +++ b/jenkins/data_source_jenkins_plugins_test.go @@ -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"), + ), + }, + }, + }) +} diff --git a/jenkins/provider.go b/jenkins/provider.go index c1b85c6..3b73b7b 100644 --- a/jenkins/provider.go +++ b/jenkins/provider.go @@ -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{