Skip to content

Commit

Permalink
Rename ec2 to s3credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Kuznetsov committed Mar 27, 2024
1 parent 2392784 commit aecb1de
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 81 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You can use this library to work with the following objects of the Selectel IAM

* [users](https://pkg.go.dev/github.com/selectel/iam-go/service/users)
* [serviceusers](https://pkg.go.dev/github.com/selectel/iam-go/service/serviceusers)
* [ec2](https://pkg.go.dev/github.com/selectel/iam-go/service/ec2)
* [s3credentials](https://pkg.go.dev/github.com/selectel/iam-go/service/s3credentials)

### Installation

Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This directory contains examples that cover various use cases and functionality for iam-go.

### Concepts
- [**Create & Delete EC2 Credentials**](./ec2-create-delete): Create a new EC2 credential for an existing Service User (ID is needed).
- [**Create & Delete S3 Credentials**](./s3credentials-create-delete): Create a new S3 Credentials for an existing Service User (ID is needed) and delete it.
- [**Create, Update & Delete Service User**](./serviceuser-create-update-delete): Create a new Service User, then update it's data and delete it.
- [**Transfer role from one User to another**](./transfer-role): Find a billing User from all and transfer it's role to another User (ID is needed).
- [**Create & Delete User**](./user-create-delete): Create a new User and delete it.
14 changes: 7 additions & 7 deletions examples/ec2-create-delete/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Create & Delete EC2 credential
# Create & Delete S3 Credentials

This example program demonstrates how to manage creating and deleting EC2 credential for a Service User.
This example program demonstrates how to manage creating and deleting S3 Credentials for a Service User.

The part of deleting a just-created credential is commented.
The part of deleting a just-created credentials is commented.

## Running this example

Running this file will execute the following operations:

1. **Create:** Create is used to create a new EC2 credential. It is implied, that the Service User ID is known.
2. **(Delete):** _(commented by default)_ Delete deletes a just-created credential on a previous step.
1. **Create:** Create is used to create a new S3 Credentials. It is implied, that the Service User ID is known.
2. **(Delete):** _(commented by default)_ Delete deletes a just-created credentials on a previous step.

You should see an output like the following (with both operations enabled):

```
Step 1: Created credential Secret Key: a1b2c3... AccessKey: 1a2b3c...
Step 2: Deleted the credential
Step 1: Created credentials Secret Key: a1b2c3... AccessKey: 1a2b3c...
Step 2: Deleted the credentials
```
27 changes: 14 additions & 13 deletions examples/ec2-create-delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func main() {
// Prefix to be added to User-Agent.
prefix := "iam-go"

// ID of the User to create EC2 credential for.
// ID of the User to create S3 Credentials for.
userID := "a1b2c3..."

// Name of the EC2 credential to create.
name := "my-ec2-credential"
// Name of the S3 Credentials to create.
name := "my-s3-credentials"

// Project ID to create the EC2 credential for.
// Project ID to create the S3 Credentials for.
projectID := "a1b2c3..."

// Create a new IAM client.
Expand All @@ -34,14 +34,14 @@ func main() {
return
}

// Get the EC2 Credentials APIinstance.
ec2CredAPI := iamClient.EC2
// Get the S3 Credentials API instance.
s3CredAPI := iamClient.S3Credentials

// Prepare an empty context.
ctx := context.Background()

// Create a new EC2 credential for the Service User.
credential, err := ec2CredAPI.Create(
// Create a new S3 Credentials for the Service User.
credentials, err := s3CredAPI.Create(
ctx,
userID,
name,
Expand All @@ -53,18 +53,19 @@ func main() {
return
}

fmt.Printf("Step 1: Created credential Secret Key: %s Access Key: %s\n", credential.SecretKey, credential.AccessKey)
fmt.Printf("Step 1: Created credentials Secret Key: %s Access Key: %s\n", credentials.SecretKey,
credentials.AccessKey)

// // Delete an existing EC2 credential.
// err = ec2CredAPI.Delete(ctx, &ec2.DeleteInput{
// // Delete an existing S3 Credentials.
// err = s3CredAPI.Delete(ctx, &s3credentials.DeleteInput{
// UserID: userID,
// AccessKey: credential.AccessKey,
// AccessKey: credentials.AccessKey,
// })

// // Handle the error.
// if err != nil {
// fmt.Println(err)
// }

// fmt.Printf("Step 2: Deleted credential")
// fmt.Printf("Step 2: Deleted credentials")
}
12 changes: 6 additions & 6 deletions iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/selectel/iam-go/iamerrors"
baseclient "github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/ec2"
"github.com/selectel/iam-go/service/s3credentials"
"github.com/selectel/iam-go/service/serviceusers"
"github.com/selectel/iam-go/service/users"
)
Expand Down Expand Up @@ -45,14 +45,14 @@ type Client struct {
// baseClient contains the configuration of the Client.
baseClient *baseclient.BaseClient

// Users instance is used to make requests against Selectel IAM API and manage panel users.
// Users instance is used to make requests against Selectel IAM API and manage Panel Users.
Users *users.Users

// ServiceUsers instance is used to make requests against Selectel IAM API and manage service users.
// ServiceUsers instance is used to make requests against Selectel IAM API and manage Service Users.
ServiceUsers *serviceusers.ServiceUsers

// EC2 instance is used to make requests against Selectel IAM API and manage EC2 credentials.
EC2 *ec2.EC2
// S3Credentials instance is used to make requests against Selectel IAM API and manage S3 Credentials.
S3Credentials *s3credentials.S3Credentials
}

type AuthOpts struct {
Expand Down Expand Up @@ -122,7 +122,7 @@ func New(opts ...Option) (*Client, error) {

c.Users = users.New(c.baseClient)
c.ServiceUsers = serviceusers.New(c.baseClient)
c.EC2 = ec2.New(c.baseClient)
c.S3Credentials = s3credentials.New(c.baseClient)

return c, nil
}
Expand Down
26 changes: 13 additions & 13 deletions iam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/selectel/iam-go/iamerrors"
baseclient "github.com/selectel/iam-go/internal/client"
"github.com/selectel/iam-go/service/ec2"
"github.com/selectel/iam-go/service/s3credentials"
"github.com/selectel/iam-go/service/serviceusers"
"github.com/selectel/iam-go/service/users"
)
Expand Down Expand Up @@ -56,10 +56,10 @@ func TestNew(t *testing.T) {
authOpts: &AuthOpts{
KeystoneToken: testToken,
},
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
EC2: ec2.New(baseClient),
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
S3Credentials: s3credentials.New(baseClient),
}
},
expectedError: nil,
Expand Down Expand Up @@ -97,10 +97,10 @@ func TestNew(t *testing.T) {
authOpts: &AuthOpts{
KeystoneToken: testToken,
},
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
EC2: ec2.New(baseClient),
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
S3Credentials: s3credentials.New(baseClient),
}
},
expectedError: nil,
Expand Down Expand Up @@ -131,10 +131,10 @@ func TestNew(t *testing.T) {
authOpts: &AuthOpts{
KeystoneToken: testToken,
},
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
EC2: ec2.New(baseClient),
baseClient: baseClient,
Users: users.New(baseClient),
ServiceUsers: serviceusers.New(baseClient),
S3Credentials: s3credentials.New(baseClient),
}
},
expectedError: nil,
Expand Down
2 changes: 0 additions & 2 deletions service/ec2/doc.go

This file was deleted.

2 changes: 2 additions & 0 deletions service/s3credentials/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package s3credentials provides a set of functions for interacting with the Selectel S3 Credentials API.
package s3credentials
34 changes: 17 additions & 17 deletions service/ec2/requests.go → service/s3credentials/requests.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ec2
package s3credentials

import (
"bytes"
Expand All @@ -13,20 +13,20 @@ import (

const apiVersion = "iam/v1"

// EC2 is used to communicate with the EC2 Credentials API.
type EC2 struct {
// S3Credentials is used to communicate with the S3 Credentials API.
type S3Credentials struct {
baseClient *client.BaseClient
}

// Initialises EC2 instance with the given client.
func New(baseClient *client.BaseClient) *EC2 {
return &EC2{
// Initialises S3Credentials instance with the given client.
func New(baseClient *client.BaseClient) *S3Credentials {
return &S3Credentials{
baseClient: baseClient,
}
}

// List returns a list of EC2 credentials for the given user.
func (ec2 *EC2) List(ctx context.Context, userID string) ([]Credential, error) {
// List returns a list of S3 Credentials for the given user.
func (s3 *S3Credentials) List(ctx context.Context, userID string) ([]Credentials, error) {
if userID == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
}
Expand All @@ -36,7 +36,7 @@ func (ec2 *EC2) List(ctx context.Context, userID string) ([]Credential, error) {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
response, err := s3.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodGet,
Path: path,
Expand All @@ -54,13 +54,13 @@ func (ec2 *EC2) List(ctx context.Context, userID string) ([]Credential, error) {
return credentials.Credentials, nil
}

// Create creates a new EC2 credential for the given user.
func (ec2 *EC2) Create(ctx context.Context, userID, name, projectID string) (*CreatedCredential, error) {
// Create creates a new S3 Credentials for the given user.
func (s3 *S3Credentials) Create(ctx context.Context, userID, name, projectID string) (*CreatedCredentials, error) {
if userID == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
}
if name == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrCredentialNameRequired, Desc: "No credential name was provided."}
return nil, iamerrors.Error{Err: iamerrors.ErrCredentialNameRequired, Desc: "No credentials name was provided."}
}
if projectID == "" {
return nil, iamerrors.Error{Err: iamerrors.ErrProjectIDRequired, Desc: "No projectID was provided."}
Expand All @@ -76,7 +76,7 @@ func (ec2 *EC2) Create(ctx context.Context, userID, name, projectID string) (*Cr
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}

response, err := ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
response, err := s3.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: bytes.NewReader(body),
Method: http.MethodPost,
Path: path,
Expand All @@ -86,16 +86,16 @@ func (ec2 *EC2) Create(ctx context.Context, userID, name, projectID string) (*Cr
return nil, err
}

var createdCredential CreatedCredential
var createdCredential CreatedCredentials
err = client.UnmarshalJSON(response, &createdCredential)
if err != nil {
return nil, iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
return &createdCredential, nil
}

// Delete deletes an EC2 credential for the given user.
func (ec2 *EC2) Delete(ctx context.Context, userID, accessKey string) error {
// Delete deletes an S3 Credentials for the given user.
func (s3 *S3Credentials) Delete(ctx context.Context, userID, accessKey string) error {
if userID == "" {
return iamerrors.Error{Err: iamerrors.ErrUserIDRequired, Desc: "No userID was provided."}
}
Expand All @@ -107,7 +107,7 @@ func (ec2 *EC2) Delete(ctx context.Context, userID, accessKey string) error {
if err != nil {
return iamerrors.Error{Err: iamerrors.ErrInternalAppError, Desc: err.Error()}
}
_, err = ec2.baseClient.DoRequest(ctx, client.DoRequestInput{
_, err = s3.baseClient.DoRequest(ctx, client.DoRequestInput{
Body: nil,
Method: http.MethodDelete,
Path: path,
Expand Down
Loading

0 comments on commit aecb1de

Please sign in to comment.