Skip to content

Commit

Permalink
Merge pull request #19 from BESTSELLER/dataResources
Browse files Browse the repository at this point in the history
Data resources
  • Loading branch information
wrighbr authored Oct 21, 2020
2 parents 0ae8da6 + 3c5fa6e commit 6b5fca4
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 2 deletions.
26 changes: 26 additions & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Data Source: harbor_project

## Example Usage
```hcl
data "haror_project" "main" {
name = "library"
}
output "project_id" {
value = data.harbor_project.main.id
}
```

## Argument Reference
The following arguments are supported:

* **name** - (Required) The of the project that will be created in harbor.

## Attributes Reference
In addition to all argument, the folloing attributes are exported:

* **project_id** - The id of the project within harbor.

* **public** - If the project will be public accessibility.

* **vulnerability_scanning** - If the images will be scanned for vulnerabilities when push to harbor.
33 changes: 33 additions & 0 deletions docs/data-sources/registry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Data Source: harbor_registry

## Example Usage

```hcl
data "harbor_registry" "main" {
name = "test_docker_harbor"
}
output "harbor_registry_id" {
value = data.harbor_registry.main.id
}
```

## Argument Reference
The following arguments are supported:

* **name** - (Required) The name of the register.

## Attributes Reference
In addition to all argument, the folloing attributes are exported:

* **registry_id** - The id of the register within harbor.

* **status** - The health status of the external container register

* **endpoint_url** - The url endpoint for the external container register

* **description** - The description of the external container register.

* **insecure** - If the certificate of the external container register can be verified.

* **type** - The type of the provider type.
74 changes: 74 additions & 0 deletions provider/data_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package provider

import (
"encoding/json"
"fmt"
"strconv"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataProject() *schema.Resource {
return &schema.Resource{
Read: dataProjectRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"project_id": {
Type: schema.TypeInt,
Computed: true,
},
"public": {
Type: schema.TypeBool,
Computed: true,
},
"vulnerability_scanning": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataProjectRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
name := d.Get("name").(string)
projectPath := models.PathProjects + "?name=" + name

resp, _, err := apiClient.SendRequest("GET", projectPath, nil, 200)
if err != nil {
return err
}

var jsonData []models.ProjectsBodyResponses
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Unable to find the project named: %s", name)
}

for _, v := range jsonData {

if v.Name == name {
id := models.PathProjects + "/" + strconv.Itoa(v.ProjectID)
public, err := strconv.ParseBool(v.Metadata.Public)
if err != nil {
return err
}
autoScan, err := strconv.ParseBool(v.Metadata.AutoScan)
if err != nil {
return err
}

d.SetId(id)
d.Set("project_id", v.ProjectID)
d.Set("name", v.Name)
d.Set("public", public)
d.Set("vulnerability_scanning", autoScan)
}
}
return nil
}
80 changes: 80 additions & 0 deletions provider/data_registry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package provider

import (
"encoding/json"
"fmt"
"strconv"

"github.com/BESTSELLER/terraform-provider-harbor/client"
"github.com/BESTSELLER/terraform-provider-harbor/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func dataRegistry() *schema.Resource {
return &schema.Resource{
Read: dataRegistryRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"registry_id": {
Type: schema.TypeInt,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
"url": {
Type: schema.TypeString,
Computed: true,
},
"insecure": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataRegistryRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*client.Client)
name := d.Get("name").(string)
registryPath := models.PathRegistries + "?name=" + name
resp, _, err := apiClient.SendRequest("GET", registryPath, nil, 200)
if err != nil {
return err
}

var jsonData []models.RegistryBody
err = json.Unmarshal([]byte(resp), &jsonData)
if err != nil {
return fmt.Errorf("Unable to find the registry named: %s", name)
}

for _, v := range jsonData {
if v.Name == name {
id := models.PathProjects + "/" + strconv.Itoa(v.ID)

d.SetId(id)
d.Set("registry_id", v.ID)
d.Set("name", v.Name)
d.Set("type", v.Type)
d.Set("description", v.Description)
d.Set("url", v.URL)
d.Set("insecure", v.Insecure)
d.Set("status", v.Status)
}
}

return nil
}
4 changes: 4 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func Provider() terraform.ResourceProvider {
"harbor_registry": resourceRegistry(),
"harbor_replication": resourceReplication(),
},
DataSourcesMap: map[string]*schema.Resource{
"harbor_project": dataProject(),
"harbor_registry": dataRegistry(),
},

ConfigureFunc: providerConfigure,
}
Expand Down
5 changes: 3 additions & 2 deletions provider/resource_robot_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ func TestAccRobotBasic(t *testing.T) {
testAccCheckResourceExists(harborRobotAccount),
resource.TestCheckResourceAttr(
harborRobotAccount, "name", "test_robot_account"),
// resource.TestCheckResourceAttr(
// harborRobotAccount, "action", "push"),
),
},
},
Expand Down Expand Up @@ -60,6 +58,9 @@ func testAccCheckRobotDestroy(s *terraform.State) error {
if rs.Type != "harbor_robot_account" {
continue
}
if rs.Type != "harbor_project" {
continue
}

resp, _, err := apiClient.SendRequest("GET", rs.Primary.ID, nil, 404)
if err != nil {
Expand Down

0 comments on commit 6b5fca4

Please sign in to comment.