-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from BESTSELLER/dataResources
Data resources
- Loading branch information
Showing
6 changed files
with
220 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters