Skip to content

Commit

Permalink
feat(iotsitewise): add portal, project, gateway, dashboard, asset, as…
Browse files Browse the repository at this point in the history
…set model and access policy
  • Loading branch information
npellegrin committed Nov 17, 2024
1 parent b830a09 commit 94d2096
Show file tree
Hide file tree
Showing 7 changed files with 760 additions and 0 deletions.
150 changes: 150 additions & 0 deletions resources/iotsitewise-access-policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iotsitewise"

"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 IoTSiteWiseAccessPolicyResource = "IoTSiteWiseAccessPolicy"

func init() {
registry.Register(&registry.Registration{
Name: IoTSiteWiseAccessPolicyResource,
Scope: nuke.Account,
Lister: &IoTSiteWiseAccessPolicyLister{},
})
}

type IoTSiteWiseAccessPolicyLister struct{}

func (l *IoTSiteWiseAccessPolicyLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { //nolint:gocyclo
opts := o.(*nuke.ListerOpts)

svc := iotsitewise.New(opts.Session)
resources := make([]resource.Resource, 0)

// Policies can be attached either to portal or projects
// List portal and portal policies
listPortalsParams := &iotsitewise.ListPortalsInput{
MaxResults: aws.Int64(25),
}
for {
listPortalsResp, err := svc.ListPortals(listPortalsParams)
if err != nil {
return nil, err
}
for _, portalItem := range listPortalsResp.PortalSummaries {
// Got portals
listProjectsParams := &iotsitewise.ListProjectsInput{
PortalId: portalItem.Id,
MaxResults: aws.Int64(25),
}

// List portal policies
listPortalPoliciesParam := &iotsitewise.ListAccessPoliciesInput{
ResourceId: portalItem.Id,
ResourceType: &([]string{string(iotsitewise.ResourceTypePortal)}[0]),
MaxResults: aws.Int64(25),
}

for {
listPortalPoliciesResp, err := svc.ListAccessPolicies(listPortalPoliciesParam)
if err != nil {
return nil, err
}
for _, item := range listPortalPoliciesResp.AccessPolicySummaries {
resources = append(resources, &IoTSiteWiseAccessPolicy{
svc: svc,
ID: item.Id,
})
}

if listPortalPoliciesResp.NextToken == nil {
break
}

listPortalPoliciesParam.NextToken = listPortalPoliciesResp.NextToken
}

// will also search inside projects
for {
listProjectsResp, err := svc.ListProjects(listProjectsParams)
if err != nil {
return nil, err
}
for _, projectItem := range listProjectsResp.ProjectSummaries {
// List project policies
listProjectPoliciesParams := &iotsitewise.ListAccessPoliciesInput{
ResourceId: projectItem.Id,
ResourceType: &([]string{string(iotsitewise.ResourceTypeProject)}[0]),
MaxResults: aws.Int64(25),
}

for {
listProjectPoliciesResp, err := svc.ListAccessPolicies(listProjectPoliciesParams)
if err != nil {
return nil, err
}
for _, item := range listProjectPoliciesResp.AccessPolicySummaries {
resources = append(resources, &IoTSiteWiseAccessPolicy{
svc: svc,
ID: item.Id,
})
}

if listProjectPoliciesResp.NextToken == nil {
break
}

listProjectPoliciesParams.NextToken = listProjectPoliciesResp.NextToken
}
}

if listProjectsResp.NextToken == nil {
break
}

listProjectsParams.NextToken = listProjectsResp.NextToken
}
}

if listPortalsResp.NextToken == nil {
break
}

listPortalsParams.NextToken = listPortalsResp.NextToken
}

return resources, nil
}

type IoTSiteWiseAccessPolicy struct {
svc *iotsitewise.IoTSiteWise
ID *string
}

func (f *IoTSiteWiseAccessPolicy) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("id", f.ID)
return properties
}

func (f *IoTSiteWiseAccessPolicy) Remove(_ context.Context) error {
_, err := f.svc.DeleteAccessPolicy(&iotsitewise.DeleteAccessPolicyInput{
AccessPolicyId: f.ID,
})

return err
}

func (f *IoTSiteWiseAccessPolicy) String() string {
return *f.ID
}
102 changes: 102 additions & 0 deletions resources/iotsitewise-asset-model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iotsitewise"

"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 IoTSiteWiseAssetModelResource = "IoTSiteWiseAssetModel"

func init() {
registry.Register(&registry.Registration{
Name: IoTSiteWiseAssetModelResource,
Scope: nuke.Account,
Lister: &IoTSiteWiseAssetModelLister{},
})
}

type IoTSiteWiseAssetModelLister struct{}

func (l *IoTSiteWiseAssetModelLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := iotsitewise.New(opts.Session)
resources := make([]resource.Resource, 0)

params := &iotsitewise.ListAssetModelsInput{
MaxResults: aws.Int64(25),
}

for {
resp, err := svc.ListAssetModels(params)
if err != nil {
return nil, err
}
for _, item := range resp.AssetModelSummaries {
tagResp, err := svc.ListTagsForResource(
&iotsitewise.ListTagsForResourceInput{
ResourceArn: item.Arn,
})
if err != nil {
return nil, err
}

resources = append(resources, &IoTSiteWiseAssetModel{
svc: svc,
ID: item.Id,
name: item.Name,
status: item.Status.State,
tags: tagResp.Tags,
})
}

if resp.NextToken == nil {
break
}

params.NextToken = resp.NextToken
}

return resources, nil
}

type IoTSiteWiseAssetModel struct {
svc *iotsitewise.IoTSiteWise
ID *string
name *string
status *string
tags map[string]*string
}

func (f *IoTSiteWiseAssetModel) Properties() types.Properties {
properties := types.NewProperties()

for key, tag := range f.tags {
properties.SetTag(&key, tag)
}

properties.Set("name", f.name)
properties.Set("status", f.status)

return properties
}

func (f *IoTSiteWiseAssetModel) Remove(_ context.Context) error {
_, err := f.svc.DeleteAssetModel(&iotsitewise.DeleteAssetModelInput{
AssetModelId: f.ID,
})

return err
}

func (f *IoTSiteWiseAssetModel) String() string {
return *f.ID
}
103 changes: 103 additions & 0 deletions resources/iotsitewise-asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iotsitewise"

"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 IoTSiteWiseAssetResource = "IoTSiteWiseAsset"

func init() {
registry.Register(&registry.Registration{
Name: IoTSiteWiseAssetResource,
Scope: nuke.Account,
Lister: &IoTSiteWiseAssetLister{},
})
}

type IoTSiteWiseAssetLister struct{}

func (l *IoTSiteWiseAssetLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := iotsitewise.New(opts.Session)
resources := make([]resource.Resource, 0)

params := &iotsitewise.ListAssetsInput{
Filter: &([]string{string(iotsitewise.ListAssetsFilterTopLevel)}[0]),
MaxResults: aws.Int64(25),
}

for {
resp, err := svc.ListAssets(params)
if err != nil {
return nil, err
}
for _, item := range resp.AssetSummaries {
tagResp, err := svc.ListTagsForResource(
&iotsitewise.ListTagsForResourceInput{
ResourceArn: item.Arn,
})
if err != nil {
return nil, err
}

resources = append(resources, &IoTSiteWiseAsset{
svc: svc,
ID: item.Id,
name: item.Name,
status: item.Status.State,
tags: tagResp.Tags,
})
}

if resp.NextToken == nil {
break
}

params.NextToken = resp.NextToken
}

return resources, nil
}

type IoTSiteWiseAsset struct {
svc *iotsitewise.IoTSiteWise
ID *string
name *string
status *string
tags map[string]*string
}

func (f *IoTSiteWiseAsset) Properties() types.Properties {
properties := types.NewProperties()

for key, tag := range f.tags {
properties.SetTag(&key, tag)
}

properties.Set("name", f.name)
properties.Set("status", f.status)

return properties
}

func (f *IoTSiteWiseAsset) Remove(_ context.Context) error {
_, err := f.svc.DeleteAsset(&iotsitewise.DeleteAssetInput{
AssetId: f.ID,
})

return err
}

func (f *IoTSiteWiseAsset) String() string {
return *f.ID
}
Loading

0 comments on commit 94d2096

Please sign in to comment.