Skip to content

ssprod_41907 | add get full policy #513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 35 additions & 17 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,24 +926,39 @@ type PosturePolicyZoneMeta struct {
}

type PosturePolicy struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type int `json:"type,omitempty"`
Kind int `json:"kind,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
Link string `json:"link,omitempty"`
Authors string `json:"authors,omitempty"`
PublishedData string `json:"publishedDate,omitempty"`
RequirementsGroup []RequirementsGroup `json:"requirementFolders,omitempty"`
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
IsCustom bool `json:"isCustom,omitempty"`
IsActive bool `json:"isActive,omitempty"`
Platform string `json:"platform,omitempty"`
Zones []PosturePolicyZoneMeta `json:"zones,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type int `json:"type,omitempty"`
Kind int `json:"kind,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
Link string `json:"link,omitempty"`
Authors string `json:"authors,omitempty"`
PublishedData string `json:"publishedDate,omitempty"`
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
IsCustom bool `json:"isCustom,omitempty"`
IsActive bool `json:"isActive,omitempty"`
Platform string `json:"platform,omitempty"`
Zones []PosturePolicyZoneMeta `json:"zones,omitempty"`
}

type FullPosturePolicy struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
Link string `json:"link,omitempty"`
Authors string `json:"authors,omitempty"`
PublishedData string `json:"publishedDate,omitempty"`
RequirementsGroup []RequirementsGroup `json:"requirementFolders,omitempty"`
MinKubeVersion float64 `json:"minKubeVersion,omitempty"`
MaxKubeVersion float64 `json:"maxKubeVersion,omitempty"`
IsCustom bool `json:"isCustom,omitempty"`
IsActive bool `json:"isActive,omitempty"`
Platform string `json:"platform,omitempty"`
}

type RequirementsGroup struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -1005,6 +1020,9 @@ type PosturePolicyResponse struct {
Data PosturePolicy `json:"data"`
}

type FullPosturePolicyResponse struct {
Data FullPosturePolicy `json:"data"`
}
type PostureZonePolicyListResponse struct {
Data []PosturePolicy `json:"data"`
}
Expand Down
15 changes: 7 additions & 8 deletions sysdig/internal/client/v2/posture_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (
const (
PosturePolicyListPath = "%s/api/cspm/v1/policy/policies/list"
PosturePolicyCreatePath = "%s/api/cspm/v1/policy"
PosturePolicyGetPath = "%s/api/cspm/v1/policy/policies/view/%d"
PosturePolicyGetPath = "%s/api/cspm/v1/policy/posture/policies/%d?include_controls=true"
)

type PosturePolicyInterface interface {
Base
ListPosturePolicies(ctx context.Context) ([]PosturePolicy, error)
CreateOrUpdatePosturePolicy(ctx context.Context, p *CreatePosturePolicy) (*PosturePolicy, string, error)
GetPosturePolicy(ctx context.Context, id int64) (*PosturePolicy, error)
CreateOrUpdatePosturePolicy(ctx context.Context, p *CreatePosturePolicy) (*FullPosturePolicy, string, error)
GetPosturePolicy(ctx context.Context, id int64) (*FullPosturePolicy, error)
}

func (client *Client) ListPosturePolicies(ctx context.Context) ([]PosturePolicy, error) {
Expand All @@ -34,7 +34,7 @@ func (client *Client) ListPosturePolicies(ctx context.Context) ([]PosturePolicy,
return resp.Data, nil
}

func (client *Client) CreateOrUpdatePosturePolicy(ctx context.Context, p *CreatePosturePolicy) (*PosturePolicy, string, error) {
func (client *Client) CreateOrUpdatePosturePolicy(ctx context.Context, p *CreatePosturePolicy) (*FullPosturePolicy, string, error) {
payload, err := Marshal(p)
if err != nil {
return nil, "", err
Expand All @@ -48,25 +48,24 @@ func (client *Client) CreateOrUpdatePosturePolicy(ctx context.Context, p *Create
errStatus, err := client.ErrorAndStatusFromResponse(response)
return nil, errStatus, err
}
resp, err := Unmarshal[PosturePolicyResponse](response.Body)
resp, err := Unmarshal[FullPosturePolicyResponse](response.Body)
if err != nil {
return nil, "", err
}
return &resp.Data, "", nil
}

func (client *Client) GetPosturePolicy(ctx context.Context, id int64) (*PosturePolicy, error) {
func (client *Client) GetPosturePolicy(ctx context.Context, id int64) (*FullPosturePolicy, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.getPolicyUrl(id), nil)
if err != nil {
return nil, err
}
defer response.Body.Close()

wrapper, err := Unmarshal[PosturePolicyResponse](response.Body)
wrapper, err := Unmarshal[FullPosturePolicyResponse](response.Body)
if err != nil {
return nil, err
}

return &wrapper.Data, nil
}

Expand Down
5 changes: 2 additions & 3 deletions sysdig/resource_sysdig_secure_posture_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ func resourceSysdigSecurePosturePolicyCreateOrUpdate(ctx context.Context, d *sch
req := &v2.CreatePosturePolicy{
ID: getStringValue(d, SchemaIDKey),
Name: getStringValue(d, SchemaNameKey),
Type: getStringValue(d, SchemaTypeKey),
Description: getStringValue(d, SchemaDescriptionKey),
MinKubeVersion: getFloatValue(d, SchemaMinKubeVersionKey),
MaxKubeVersion: getFloatValue(d, SchemaMaxKubeVersionKey),
Expand Down Expand Up @@ -249,9 +250,7 @@ func resourceSysdigSecurePosturePolicyRead(ctx context.Context, d *schema.Resour
return diag.FromErr(err)
}

strconv.Itoa(policy.Type)

err = d.Set(SchemaTypeKey, strconv.Itoa(policy.Type))
err = d.Set(SchemaTypeKey, policy.Type)
if err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_secure_posture_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func createPolicyResource(name string) string {
name = "policy-test-%s"
description = "policy description"
is_active = true
type = "0"
type = "Unknown"
}`, name)
}
Loading