diff --git a/README.md b/README.md index 33690bc..4fd59ce 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,23 @@ $ terraform import bitbucketserver_admin_mail_server.mail mail.example.com --- +## Data Sources + +### Application Properties + +```hcl +data "bitbucketserver_application_properties" "main" {} +``` + +#### Attributes + +* `version` - Version of Bitbucket. +* `build_number` - Build number of the Bitbucket instance. +* `build_date` - Date the Bitbucket build was made, +* `display_name` - Name of the Bitbucket instance. + +--- + ## Development Guide ### Requirements diff --git a/bitbucket/data_application_properties.go b/bitbucket/data_application_properties.go new file mode 100644 index 0000000..1675dfb --- /dev/null +++ b/bitbucket/data_application_properties.go @@ -0,0 +1,71 @@ +package bitbucket + +import ( + "encoding/json" + "github.com/hashicorp/terraform/helper/schema" + "io/ioutil" +) + +type ApplicationProperties struct { + Version string `json:"version,omitempty"` + BuildNumber string `json:"buildNumber,omitempty"` + BuildDate string `json:"buildDate,omitempty"` + DisplayName string `json:"displayName,omitempty"` +} + +func dataSourceApplicationProperties() *schema.Resource { + return &schema.Resource{ + Read: dataSourceApplicationPropertiesRead, + + Schema: map[string]*schema.Schema{ + "version": { + Type: schema.TypeString, + Computed: true, + }, + "build_number": { + Type: schema.TypeString, + Computed: true, + }, + "build_date": { + Type: schema.TypeString, + Computed: true, + }, + "display_name": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceApplicationPropertiesRead(d *schema.ResourceData, m interface{}) error { + client := m.(*BitbucketClient) + req, err := client.Get("/rest/api/1.0/application-properties") + + if err != nil { + return err + } + + if req.StatusCode == 200 { + + var applicationProperties ApplicationProperties + + body, readerr := ioutil.ReadAll(req.Body) + if readerr != nil { + return readerr + } + + decodeerr := json.Unmarshal(body, &applicationProperties) + if decodeerr != nil { + return decodeerr + } + + d.SetId(applicationProperties.Version) + d.Set("version", applicationProperties.Version) + d.Set("build_number", applicationProperties.BuildNumber) + d.Set("build_date", applicationProperties.BuildDate) + d.Set("display_name", applicationProperties.DisplayName) + } + + return nil +} \ No newline at end of file diff --git a/bitbucket/data_application_properties_test.go b/bitbucket/data_application_properties_test.go new file mode 100644 index 0000000..b119025 --- /dev/null +++ b/bitbucket/data_application_properties_test.go @@ -0,0 +1,29 @@ +package bitbucket + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccBitbucketDataApplicationProperties(t *testing.T) { + config := ` + data "bitbucketserver_application_properties" "main" {} + ` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.bitbucketserver_application_properties.main", "version", "6.7.0"), + resource.TestCheckResourceAttr("data.bitbucketserver_application_properties.main", "build_number", "6007000"), + resource.TestCheckResourceAttr("data.bitbucketserver_application_properties.main", "build_date", "1569809627115"), + resource.TestCheckResourceAttr("data.bitbucketserver_application_properties.main", "display_name", "Bitbucket"), + ), + }, + }, + }) +} diff --git a/bitbucket/provider.go b/bitbucket/provider.go index 2ad62cb..b85286d 100644 --- a/bitbucket/provider.go +++ b/bitbucket/provider.go @@ -28,6 +28,9 @@ func Provider() terraform.ResourceProvider { }, }, ConfigureFunc: providerConfigure, + DataSourcesMap: map[string]*schema.Resource{ + "bitbucketserver_application_properties": dataSourceApplicationProperties(), + }, ResourcesMap: map[string]*schema.Resource{ "bitbucketserver_admin_license": resourceAdminLicense(), "bitbucketserver_admin_mail_server": resourceAdminMailServer(),