Skip to content

Commit

Permalink
release v1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
contabo committed Jan 12, 2023
1 parent b9daa6e commit 6cec065
Show file tree
Hide file tree
Showing 66 changed files with 4,311 additions and 1,458 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ release: build
go get github.com/mitchellh/gox
mkdir -p dist/
rm -rf dist/*
export VERSION=$$(git rev-list --tags --max-count=1 | xargs -I {} git describe --tags {}); export COMMIT=$$(git rev-parse HEAD); export TIMESTAMP=$$(date -u +"%Y-%m-%dT%H:%M:%SZ"); gox -osarch="darwin/amd64 linux/amd64 linux/arm64 windows/amd64 darwin/arm64" -ldflags="-w -s -X \"contabo.com/cli/cntb/cmd.version=$$VERSION\" -X \"contabo.com/cli/cntb/cmd.commit=$$COMMIT\" -X \"contabo.com/cli/cntb/cmd.date=$$TIMESTAMP\"" -output="dist/{{.OS}}_{{.Arch}}/{{.Dir}}"
export VERSION=$$(git rev-list --tags --max-count=1 | xargs -I {} git describe --tags {}); export COMMIT=$$(git rev-parse HEAD); export TIMESTAMP=$$(date -u +"%Y-%m-%dT%H:%M:%SZ"); gox -osarch="darwin/amd64 linux/amd64 linux/arm64 windows/amd64 darwin/arm64" -ldflags="-w -s -X \"contabo.com/cli/cntb/cmd.version=$$VERSION\" -X \"contabo.com/cli/cntb/cmd.commit=$$COMMIT\" -X \"contabo.com/cli/cntb/cmd.date=$$TIMESTAMP\"" -output="dist/{{.OS}}_{{.Arch}}/{{.Dir}}"
69 changes: 24 additions & 45 deletions cmd/buckets/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,39 @@ import (
contaboCmd "contabo.com/cli/cntb/cmd"
"contabo.com/cli/cntb/cmd/util"
"contabo.com/cli/cntb/config"
authClient "contabo.com/cli/cntb/oauth2Client"

jwt "github.com/golang-jwt/jwt"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var createBucketCmd = &cobra.Command{
Use: "bucket [region] [bucketName]",
Use: "bucket --storageId [objectStorageId] --name [bucketName]",
Short: "Create a bucket.",
Long: `Create a bucket with the given name. See https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-s3-bucket-naming-requirements.html for naming conventions.`,
Example: `cntb create bucket EU bucket123`,
Example: `cntb create bucket --storageId 1de451d4-9737-487d-9e5d-8f50a6c8d558 --name m123`,
Run: func(cmd *cobra.Command, args []string) {

// get list of object storage
ApiRetrieveObjectStorageListRequest := client.ApiClient().
ObjectStoragesApi.RetrieveObjectStorageList(context.Background()).
XRequestId(uuid.NewV4().String()).
Page(contaboCmd.Page).
Size(contaboCmd.Size)

ApiRetrieveObjectStorageListRequest = ApiRetrieveObjectStorageListRequest.Region(createRegion)

objStorageListresponse, httpResp, err := ApiRetrieveObjectStorageListRequest.Execute()
util.HandleErrors(err, httpResp, "while retrieving object storages")

if len(objStorageListresponse.Data) == 0 {
log.Fatal("No Object Storage could be found in this region.")
}
ApiRetrieveObjectStorageRequest := client.ApiClient().
ObjectStoragesApi.RetrieveObjectStorage(context.Background(), createBucketObjectStorageId).
XRequestId(uuid.NewV4().String())

objStorage := objStorageListresponse.Data[0]
objStorageRetrieveResponse, httpResp, err := ApiRetrieveObjectStorageRequest.Execute()
util.HandleErrors(err, httpResp, fmt.Sprintf("Error while retrieving object storage with id : %v", createBucketObjectStorageId))

// get keycloakId from jwt Token
jwtAccessToken := authClient.RestoreTokenFromCache(config.Conf.Oauth2User).AccessToken
claims := jwt.MapClaims{}
_, err = jwt.ParseWithClaims(jwtAccessToken, claims, func(token *jwt.Token) (interface{}, error) {
return []byte("<YOUR VERIFICATION KEY>"), nil
})
if err != nil {
log.Debug(err)
if len(objStorageRetrieveResponse.Data) == 0 {
log.Fatal(fmt.Sprintf("No Object Storage could be found with id : %v", createBucketObjectStorageId))
}

if claims["sub"] == nil {
log.Fatal("Error in getting access token.")
}
keycloakId := claims["sub"]
objStorage := objStorageRetrieveResponse.Data[0]

keycloakId := util.GetKeycloakId(config.Conf.Oauth2User)
// get user credentials
ApiGetObjectStorageCredentialsRequest := client.ApiClient().UsersApi.GetObjectStorageCredentials(context.Background(), keycloakId.(string)).
XRequestId(uuid.NewV4().String())
retrieveCredentialResponse, httpResp, err := ApiGetObjectStorageCredentialsRequest.Execute()
util.HandleErrors(err, httpResp, "while retrieving credentials")
retrieveCredentialResponse, httpResp, err := util.GetObjectStorageCredentials(keycloakId, createBucketObjectStorageId)
util.HandleErrors(err, httpResp, fmt.Sprintf("Error while getting credentials for object storage with id : %v", createBucketObjectStorageId))
awsAccessKeyCred := retrieveCredentialResponse.Data[0].AccessKey
awsSecretKeyCred := retrieveCredentialResponse.Data[0].SecretKey

Expand Down Expand Up @@ -104,22 +82,20 @@ var createBucketCmd = &cobra.Command{
Args: func(cmd *cobra.Command, args []string) error {
contaboCmd.ValidateCreateInput()

if len(args) > 2 {
if len(args) > 0 {
cmd.Help()
log.Fatal("Too many positional arguments.")
}

if len(args) < 2 {
cmd.Help()
log.Fatal("Please provide a region and a bucketName.")
}
viper.BindPFlag("storageId", cmd.Flags().Lookup("storageId"))
createBucketObjectStorageId = viper.GetString("storageId")

createRegion = args[0]
createBucketName = args[1]
viper.BindPFlag("name", cmd.Flags().Lookup("name"))
createBucketName = viper.GetString("name")

if createRegion == "" {
if createBucketObjectStorageId == "" {
cmd.Help()
log.Fatal("Argument region is empty. Please provide a non empty region.")
log.Fatal("Argument storageId is empty. Please provide a non empty storageId.")
}

if createBucketName == "" {
Expand All @@ -133,4 +109,7 @@ var createBucketCmd = &cobra.Command{

func init() {
contaboCmd.CreateCmd.AddCommand(createBucketCmd)
createBucketCmd.Flags().StringVarP(&createBucketObjectStorageId, "storageId", "s", "", `Id of the objectStorage where the bucket will be created`)
createBucketCmd.Flags().StringVarP(&createBucketName, "name", "n", "", `Name of the bucket that will be created`)

}
68 changes: 24 additions & 44 deletions cmd/buckets/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,41 @@ import (
"net/url"

"contabo.com/cli/cntb/client"
"contabo.com/cli/cntb/config"
contaboCmd "contabo.com/cli/cntb/cmd"
"contabo.com/cli/cntb/cmd/util"
authClient "contabo.com/cli/cntb/oauth2Client"
jwt "github.com/golang-jwt/jwt"
"contabo.com/cli/cntb/config"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var deleteBucketsCmd = &cobra.Command{
Use: "bucket [region] [bucketName]",
Use: "bucket --storageId [objectStorageId] --name [bucketName]",
Short: "Delete a specific bucket.",
Long: `Delete a bucket you created by its name.`,
Example: `cntb delete bucket EU bucket123`,
Example: `cntb delete bucket EU --storageId 1de451d4-9737-487d-9e5d-8f50a6c8d558 --name m123`,
Run: func(cmd *cobra.Command, args []string) {
// get list of object storage
ApiRetrieveObjectStorageListRequest := client.ApiClient().
ObjectStoragesApi.RetrieveObjectStorageList(context.Background()).
XRequestId(uuid.NewV4().String()).
Page(contaboCmd.Page).
Size(contaboCmd.Size)

ApiRetrieveObjectStorageListRequest = ApiRetrieveObjectStorageListRequest.Region(deleteRegion)
ApiRetrieveObjectStorageRequest := client.ApiClient().
ObjectStoragesApi.RetrieveObjectStorage(context.Background(), deleteBucketObjectStorageId).
XRequestId(uuid.NewV4().String())

objStorageListresponse, httpResp, err := ApiRetrieveObjectStorageListRequest.Execute()
util.HandleErrors(err, httpResp, "while retrieving object storages")
objStorageRetrieveResponse, httpResp, err := ApiRetrieveObjectStorageRequest.Execute()
util.HandleErrors(err, httpResp, fmt.Sprintf("Error while retrieving object storage with id : %v", deleteBucketObjectStorageId))

if len(objStorageListresponse.Data) == 0 {
log.Fatal("No Object Storage could be found in this region.")
if len(objStorageRetrieveResponse.Data) == 0 {
log.Fatal(fmt.Sprintf("No Object Storage could be found with id : %v", deleteBucketObjectStorageId))
}

objStorage := objStorageListresponse.Data[0]
objStorage := objStorageRetrieveResponse.Data[0]

// get keycloakId from jwt Token
jwtAccessToken := authClient.RestoreTokenFromCache(config.Conf.Oauth2User).AccessToken
claims := jwt.MapClaims{}
_, err = jwt.ParseWithClaims(jwtAccessToken, claims, func(token *jwt.Token) (interface{}, error) {
return []byte("<YOUR VERIFICATION KEY>"), nil
})
if err != nil {
log.Debug(err)
}
if claims["sub"] == nil {
log.Fatal("Error in getting access token.")
}
keycloakId := claims["sub"]
keycloakId := util.GetKeycloakId(config.Conf.Oauth2User)

// get user credentials
ApiGetObjectStorageCredentialsRequest := client.ApiClient().UsersApi.GetObjectStorageCredentials(context.Background(), keycloakId.(string)).
XRequestId(uuid.NewV4().String())
retrieveCredentialResponse, httpResp, err := ApiGetObjectStorageCredentialsRequest.Execute()
util.HandleErrors(err, httpResp, "while retrieving credentials")
retrieveCredentialResponse, httpResp, err := util.GetObjectStorageCredentials(keycloakId, deleteBucketObjectStorageId)
util.HandleErrors(err, httpResp, fmt.Sprintf("Error while getting credentials for object storage with id : %v", deleteBucketObjectStorageId))
awsAccessKeyCred := retrieveCredentialResponse.Data[0].AccessKey
awsSecretKeyCred := retrieveCredentialResponse.Data[0].SecretKey

Expand Down Expand Up @@ -91,22 +71,20 @@ var deleteBucketsCmd = &cobra.Command{
fmt.Printf("Bucket " + deleteBucketName + " deleted.\n")
},
Args: func(cmd *cobra.Command, args []string) error {
if len(args) > 2 {
if len(args) > 0 {
cmd.Help()
log.Fatal("Too many positional arguments.")
}

if len(args) < 2 {
cmd.Help()
log.Fatal("Please provide a region and a bucketName.")
}
viper.BindPFlag("storageId", cmd.Flags().Lookup("storageId"))
deleteBucketObjectStorageId = viper.GetString("storageId")

deleteRegion = args[0]
deleteBucketName = args[1]
viper.BindPFlag("name", cmd.Flags().Lookup("name"))
deleteBucketName = viper.GetString("name")

if deleteRegion == "" {
if deleteBucketObjectStorageId == "" {
cmd.Help()
log.Fatal("Argument region is empty. Please provide a non empty region.")
log.Fatal("Argument region is empty. Please provide a non empty storage id.")
}
if deleteBucketName == "" {
cmd.Help()
Expand All @@ -119,4 +97,6 @@ var deleteBucketsCmd = &cobra.Command{

func init() {
contaboCmd.DeleteCmd.AddCommand(deleteBucketsCmd)
deleteBucketsCmd.Flags().StringVarP(&deleteBucketObjectStorageId, "storageId", "s", "", `Id of the objectStorage where the bucket will be deleted`)
deleteBucketsCmd.Flags().StringVarP(&deleteBucketName, "name", "n", "", `Name of the bucket that will be deleted`)
}
Loading

0 comments on commit 6cec065

Please sign in to comment.