1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
package sweepers
import (
"errors"
"fmt"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/internal/testhelpers"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func SweepUser(scwClient *scw.Client) error {
api := iam.NewAPI(scwClient)
orgID, exists := scwClient.GetDefaultOrganizationID()
if !exists {
return errors.New("missing organizationID")
}
listUsers, err := api.ListUsers(&iam.ListUsersRequest{
OrganizationID: &orgID,
})
if err != nil {
return fmt.Errorf("failed to list users: %w", err)
}
for _, user := range listUsers.Users {
if !testhelpers.IsTestResource(user.Email) {
continue
}
err = api.DeleteUser(&iam.DeleteUserRequest{
UserID: user.ID,
})
if err != nil {
return fmt.Errorf("failed to delete user: %w", err)
}
}
return nil
}
func SweepSSHKey(scwClient *scw.Client) error {
iamAPI := iam.NewAPI(scwClient)
logger.Warningf("sweeper: destroying the SSH keys")
listSSHKeys, err := iamAPI.ListSSHKeys(&iam.ListSSHKeysRequest{}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error listing SSH keys in sweeper: %s", err)
}
for _, sshKey := range listSSHKeys.SSHKeys {
err := iamAPI.DeleteSSHKey(&iam.DeleteSSHKeyRequest{
SSHKeyID: sshKey.ID,
})
if err != nil {
return fmt.Errorf("error deleting SSH key in sweeper: %s", err)
}
}
return nil
}
func SweepPolicy(scwClient *scw.Client) error {
api := iam.NewAPI(scwClient)
orgID, exists := scwClient.GetDefaultOrganizationID()
if !exists {
return errors.New("missing organizationID")
}
listPols, err := api.ListPolicies(&iam.ListPoliciesRequest{
OrganizationID: orgID,
})
if err != nil {
return fmt.Errorf("failed to list policies: %w", err)
}
for _, pol := range listPols.Policies {
if !testhelpers.IsTestResource(pol.Name) {
continue
}
err = api.DeletePolicy(&iam.DeletePolicyRequest{
PolicyID: pol.ID,
})
if err != nil {
return fmt.Errorf("failed to delete policy: %w", err)
}
}
return nil
}
func SweepGroup(scwClient *scw.Client) error {
api := iam.NewAPI(scwClient)
orgID, exists := scwClient.GetDefaultOrganizationID()
if !exists {
return errors.New("missing organizationID")
}
listApps, err := api.ListGroups(&iam.ListGroupsRequest{
OrganizationID: orgID,
})
if err != nil {
return fmt.Errorf("failed to list groups: %w", err)
}
for _, group := range listApps.Groups {
if !testhelpers.IsTestResource(group.Name) {
continue
}
err = api.DeleteGroup(&iam.DeleteGroupRequest{
GroupID: group.ID,
})
if err != nil {
return fmt.Errorf("failed to delete group: %w", err)
}
}
return nil
}
func SweepApplication(scwClient *scw.Client) error {
api := iam.NewAPI(scwClient)
orgID, exists := scwClient.GetDefaultOrganizationID()
if !exists {
return errors.New("missing organizationID")
}
listApps, err := api.ListApplications(&iam.ListApplicationsRequest{
OrganizationID: orgID,
})
if err != nil {
return fmt.Errorf("failed to list applications: %w", err)
}
for _, app := range listApps.Applications {
if !testhelpers.IsTestResource(app.Name) {
continue
}
err = api.DeleteApplication(&iam.DeleteApplicationRequest{
ApplicationID: app.ID,
})
if err != nil {
return fmt.Errorf("failed to delete application: %w", err)
}
}
return nil
}
func SweepAPIKey(scwClient *scw.Client) error {
api := iam.NewAPI(scwClient)
logger.Debugf("sweeper: destroying the api keys")
orgID, exists := scwClient.GetDefaultOrganizationID()
if !exists {
return errors.New("missing organizationID")
}
listAPIKeys, err := api.ListAPIKeys(&iam.ListAPIKeysRequest{
OrganizationID: &orgID,
}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("failed to list api keys: %w", err)
}
for _, key := range listAPIKeys.APIKeys {
if !testhelpers.IsTestResource(key.Description) {
continue
}
err = api.DeleteAPIKey(&iam.DeleteAPIKeyRequest{
AccessKey: key.AccessKey,
})
if err != nil {
return fmt.Errorf("failed to delete api key: %w", err)
}
}
return nil
}
func SweepAll(scwClient *scw.Client) error {
if err := SweepUser(scwClient); err != nil {
return err
}
if err := SweepSSHKey(scwClient); err != nil {
return err
}
if err := SweepPolicy(scwClient); err != nil {
return err
}
if err := SweepGroup(scwClient); err != nil {
return err
}
if err := SweepApplication(scwClient); err != nil {
return err
}
if err := SweepAPIKey(scwClient); err != nil {
return err
}
return nil
}
|