-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bedrock): add guardrails and evaluation jobs
- Loading branch information
1 parent
674525d
commit f72bc7e
Showing
2 changed files
with
202 additions
and
0 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,105 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/bedrock" | ||
|
||
"github.com/ekristen/libnuke/pkg/registry" | ||
"github.com/ekristen/libnuke/pkg/resource" | ||
"github.com/ekristen/libnuke/pkg/types" | ||
|
||
"github.com/ekristen/aws-nuke/v3/pkg/nuke" | ||
) | ||
|
||
const BedrockEvaluationJobResource = "BedrockEvaluationJob" | ||
|
||
func init() { | ||
registry.Register(®istry.Registration{ | ||
Name: BedrockEvaluationJobResource, | ||
Scope: nuke.Account, | ||
Lister: &BedrockEvaluationJobLister{}, | ||
}) | ||
} | ||
|
||
type BedrockEvaluationJobLister struct{} | ||
|
||
func (l *BedrockEvaluationJobLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { | ||
opts := o.(*nuke.ListerOpts) | ||
|
||
svc := bedrock.New(opts.Session) | ||
resources := make([]resource.Resource, 0) | ||
|
||
params := &bedrock.ListEvaluationJobsInput{ | ||
MaxResults: aws.Int64(30), | ||
} | ||
|
||
for { | ||
resp, err := svc.ListEvaluationJobs(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, jobSummary := range resp.JobSummaries { | ||
tagResp, err := svc.ListTagsForResource( | ||
&bedrock.ListTagsForResourceInput{ | ||
ResourceARN: jobSummary.JobArn, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resources = append(resources, &BedrockEvaluationJob{ | ||
svc: svc, | ||
Arn: jobSummary.JobArn, | ||
Name: jobSummary.JobName, | ||
Status: jobSummary.Status, | ||
Tags: tagResp.Tags, | ||
}) | ||
} | ||
|
||
if resp.NextToken == nil { | ||
break | ||
} | ||
|
||
params.NextToken = resp.NextToken | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
type BedrockEvaluationJob struct { | ||
svc *bedrock.Bedrock | ||
Arn *string | ||
Name *string | ||
Status *string | ||
Tags []*bedrock.Tag | ||
} | ||
|
||
func (f *BedrockEvaluationJob) Remove(_ context.Context) error { | ||
// We cannot delete an evaluation job from API, only stop it | ||
// Deletion seems to be possible in console only for now | ||
_, err := f.svc.StopEvaluationJob(&bedrock.StopEvaluationJobInput{ | ||
JobIdentifier: f.Arn, | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func (r *BedrockEvaluationJob) String() string { | ||
return *r.Name | ||
} | ||
|
||
func (r *BedrockEvaluationJob) Properties() types.Properties { | ||
return types.NewPropertiesFromStruct(r) | ||
} | ||
|
||
func (r *BedrockEvaluationJob) Filter() error { | ||
if *r.Status != bedrock.EvaluationJobStatusInProgress { | ||
// May be completed, failed, stopping or stopped | ||
return fmt.Errorf("already stopped") | ||
} | ||
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,97 @@ | ||
package resources | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/bedrock" | ||
|
||
"github.com/ekristen/libnuke/pkg/registry" | ||
"github.com/ekristen/libnuke/pkg/resource" | ||
"github.com/ekristen/libnuke/pkg/types" | ||
|
||
"github.com/ekristen/aws-nuke/v3/pkg/nuke" | ||
) | ||
|
||
const BedrockGuardrailResource = "BedrockGuardrail" | ||
|
||
func init() { | ||
registry.Register(®istry.Registration{ | ||
Name: BedrockGuardrailResource, | ||
Scope: nuke.Account, | ||
Lister: &BedrockGuardrailLister{}, | ||
}) | ||
} | ||
|
||
type BedrockGuardrailLister struct{} | ||
|
||
func (l *BedrockGuardrailLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { | ||
opts := o.(*nuke.ListerOpts) | ||
|
||
svc := bedrock.New(opts.Session) | ||
resources := make([]resource.Resource, 0) | ||
|
||
params := &bedrock.ListGuardrailsInput{ | ||
MaxResults: aws.Int64(30), | ||
} | ||
|
||
for { | ||
resp, err := svc.ListGuardrails(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, guardrail := range resp.Guardrails { | ||
tagResp, err := svc.ListTagsForResource( | ||
&bedrock.ListTagsForResourceInput{ | ||
ResourceARN: guardrail.Arn, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resources = append(resources, &BedrockGuardrail{ | ||
svc: svc, | ||
ID: guardrail.Id, | ||
Version: guardrail.Version, | ||
Name: guardrail.Name, | ||
Status: guardrail.Status, | ||
Tags: tagResp.Tags, | ||
}) | ||
} | ||
|
||
if resp.NextToken == nil { | ||
break | ||
} | ||
|
||
params.NextToken = resp.NextToken | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
type BedrockGuardrail struct { | ||
svc *bedrock.Bedrock | ||
ID *string | ||
Version *string | ||
Name *string | ||
Status *string | ||
Tags []*bedrock.Tag | ||
} | ||
|
||
func (r *BedrockGuardrail) String() string { | ||
return *r.ID | ||
} | ||
|
||
func (r *BedrockGuardrail) Remove(_ context.Context) error { | ||
// When guardrail version is not specified, all versions are deleted | ||
_, err := r.svc.DeleteGuardrail(&bedrock.DeleteGuardrailInput{ | ||
GuardrailIdentifier: r.ID, | ||
}) | ||
|
||
return err | ||
} | ||
|
||
func (r *BedrockGuardrail) Properties() types.Properties { | ||
return types.NewPropertiesFromStruct(r) | ||
} |