-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
group_metadata
get multiple metadata about NeuVector group me…
…mbers
- Loading branch information
Showing
14 changed files
with
193 additions
and
120 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
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,35 @@ | ||
--- | ||
# generated by https://github.com/hashicorp/terraform-plugin-docs | ||
page_title: "neuvector_group_metadata Data Source - terraform-provider-neuvector" | ||
subcategory: "" | ||
description: |- | ||
--- | ||
|
||
# neuvector_group_metadata (Data Source) | ||
|
||
|
||
|
||
## Example Usage | ||
|
||
```terraform | ||
data "neuvector_group_metadata" "test" { | ||
name = "nodes" | ||
} | ||
``` | ||
|
||
<!-- schema generated by tfplugindocs --> | ||
## Schema | ||
|
||
### Required | ||
|
||
- `name` (String) The name of the gorup. | ||
|
||
### Read-Only | ||
|
||
- `container_ids` (Set of String) List of every container id in the group. | ||
- `id` (String) The ID of this resource. | ||
- `image_ids` (Set of String) List of every image id in the group. | ||
- `services` (Set of String) List of every service name in the group. | ||
|
||
|
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
examples/data-sources/neuvector_group_metadata/data-source.tf
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,3 @@ | ||
data "neuvector_group_metadata" "test" { | ||
name = "nodes" | ||
} |
3 changes: 0 additions & 3 deletions
3
examples/data-sources/neuvector_group_services/data-source.tf
This file was deleted.
Oops, something went wrong.
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
140 changes: 140 additions & 0 deletions
140
internal/resources/neuvector/data_source_group_metadata.go
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,140 @@ | ||
// data_source_group_services.go | ||
package neuvector | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
goneuvector "github.com/theobori/go-neuvector/neuvector" | ||
"github.com/theobori/go-neuvector/util" | ||
) | ||
|
||
var AllowedMetadata = []string{ | ||
"services", | ||
"container_ids", | ||
"image_ids", | ||
} | ||
|
||
var dataGroupMetadataSchema = map[string]*schema.Schema{ | ||
"services": { | ||
Type: schema.TypeSet, | ||
Description: "List of every service name in the group.", | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"container_ids": { | ||
Type: schema.TypeSet, | ||
Description: "List of every container id in the group.", | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"image_ids": { | ||
Type: schema.TypeSet, | ||
Description: "List of every image id in the group.", | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "The name of the gorup.", | ||
Required: true, | ||
}, | ||
} | ||
|
||
func DataSourceGroupMetadata() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceGroupMetadataRead, | ||
Schema: dataGroupMetadataSchema, | ||
} | ||
} | ||
|
||
func getMemberFieldFromString(m *goneuvector.WorkloadBrief, s string) (string, error) { | ||
var ret string | ||
|
||
switch s { | ||
case "services": | ||
ret = m.Service | ||
case "container_ids": | ||
ret = m.ID | ||
case "image_ids": | ||
ret = m.ImageID | ||
default: | ||
return ret, fmt.Errorf("invalid key") | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func addGroupInfo( | ||
infos *map[string][]string, | ||
m *goneuvector.WorkloadBrief, | ||
key string, | ||
) error { | ||
value, err := getMemberFieldFromString(m, key) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
exists, _ := util.ItemExists( | ||
(*infos)[key], | ||
value, | ||
) | ||
|
||
if exists { | ||
return fmt.Errorf("already exists") | ||
} | ||
|
||
(*infos)[key] = append((*infos)[key], value) | ||
|
||
return nil | ||
} | ||
|
||
func addGroupMetadata( | ||
infos *map[string][]string, | ||
m *goneuvector.WorkloadBrief, | ||
keys ...string, | ||
) { | ||
for _, key := range keys { | ||
// We do not need to handle the error there | ||
addGroupInfo(infos, m, key) | ||
} | ||
} | ||
|
||
func dataSourceGroupMetadataRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
infos := map[string][]string{} | ||
|
||
APIClient := meta.(*goneuvector.Client) | ||
|
||
name := d.Get("name").(string) | ||
group, err := APIClient. | ||
WithContext(ctx). | ||
GetGroup(name) | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
members := group.Group.Members | ||
|
||
for _, m := range members { | ||
addGroupMetadata(&infos, &m, AllowedMetadata...) | ||
} | ||
|
||
for k, v := range infos { | ||
d.Set(k, v) | ||
} | ||
|
||
id, err := uuid.GenerateUUID() | ||
|
||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(id) | ||
|
||
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
69 changes: 0 additions & 69 deletions
69
internal/resources/neuvector/data_source_group_services.go
This file was deleted.
Oops, something went wrong.
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
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
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