Skip to content

Commit

Permalink
Add ecs-task screen
Browse files Browse the repository at this point in the history
Add ecs-container screen
  • Loading branch information
RamanaReddy0M committed Nov 22, 2023
1 parent 54722b2 commit 41fe621
Show file tree
Hide file tree
Showing 13 changed files with 626 additions and 86 deletions.
112 changes: 110 additions & 2 deletions internal/aws/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/ecs/types"
)

// --- ECS Clusters ---
Expand Down Expand Up @@ -115,7 +117,7 @@ func GetEcsServiceJSONResponse(cfg aws.Config, clusterName, serviceName string)
ecsClient := ecs.NewFromConfig(cfg)
// Describe the specific service within the cluster
describeServicesInput := &ecs.DescribeServicesInput{
Cluster: &clusterName,
Cluster: &clusterName,
Services: []string{serviceName},
}
result, err := ecsClient.DescribeServices(context.TODO(), describeServicesInput)
Expand All @@ -133,4 +135,110 @@ func GetEcsServiceJSONResponse(cfg aws.Config, clusterName, serviceName string)
return "", err
}
return string(jsonResponse), nil
}
}

// --- ECS Tasks ---

func ListEcsTasks(cfg aws.Config, clusterName, serviceName string) ([]EcsTaskResp, error) {
ecsClient := ecs.NewFromConfig(cfg)
taskDetails, err := DescribeEcsTasksForService(ecsClient, clusterName, serviceName)
if err != nil {
return nil, err
}
tasks := make([]EcsTaskResp, len(taskDetails.Tasks))
for i, task := range taskDetails.Tasks {
t := &EcsTaskResp{
TaskId: GetTaskIDFromArn(*task.TaskArn),
Task: &task,
}
tasks[i] = *t
}
return tasks, nil
}

func DescribeEcsTasksForService(ecsClient *ecs.Client, clusterName, serviceName string) (*ecs.DescribeTasksOutput, error) {
listTasksInput := &ecs.ListTasksInput{
Cluster: &clusterName,
ServiceName: &serviceName,
}

result, err := ecsClient.ListTasks(context.TODO(), listTasksInput)
if err != nil {
return nil, err
}

describeTasksInput := &ecs.DescribeTasksInput{
Cluster: &clusterName,
Tasks: result.TaskArns,
}

taskDetails, err := ecsClient.DescribeTasks(context.TODO(), describeTasksInput)
if err != nil {
return nil, err
}

return taskDetails, nil
}

func GetTaskJSONResponse(cfg aws.Config, clusterName, taskArn string) (string, error) {
ecsClient := ecs.NewFromConfig(cfg)
describeTasksInput := &ecs.DescribeTasksInput{
Cluster: &clusterName,
Tasks: []string{GetTaskIDFromArn(taskArn)},
}
taskDetails, err := ecsClient.DescribeTasks(context.TODO(), describeTasksInput)
if err != nil {
return "", err
}
// Convert containers to JSON
jsonResponse, err := json.MarshalIndent(taskDetails.Tasks[0], "", " ")
if err != nil {
return "", err
}
return string(jsonResponse), nil
}

// --- ECS Containers ---

func ListContainersForTask(cfg aws.Config, clusterName, taskId string) ([]types.Container, error) {
ecsClient := ecs.NewFromConfig(cfg)
describeTasksInput := &ecs.DescribeTasksInput{
Cluster: &clusterName,
Tasks: []string{taskId},
}
taskDetails, err := ecsClient.DescribeTasks(context.TODO(), describeTasksInput)
if err != nil {
return nil, err
}
if len(taskDetails.Tasks) == 0 {
return nil, fmt.Errorf("task with ID %s not found", taskId)
}
return taskDetails.Tasks[0].Containers, nil
}

func GetECSContainerJsonResponse(cfg aws.Config, clusterName, taskId, runtimeId string) (string, error) {
ecsClient := ecs.NewFromConfig(cfg)
describeTasksInput := &ecs.DescribeTasksInput{
Cluster: &clusterName,
Tasks: []string{taskId},
}
taskDetails, err := ecsClient.DescribeTasks(context.Background(), describeTasksInput)
if err != nil {
return "", err
}
for _, container := range taskDetails.Tasks[0].Containers {
if *container.RuntimeId == runtimeId {
jsonResponse, err := json.MarshalIndent(container, "", " ")
if err != nil {
return "", err
}
return string(jsonResponse), nil
}
}
return "", fmt.Errorf("container %s not found in task %s", runtimeId, taskId)
}

func GetTaskIDFromArn(taskArn string) string {
parts := strings.Split(taskArn, "/")
return parts[len(parts)-1]
}
20 changes: 13 additions & 7 deletions internal/aws/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go/service/ec2"
)
Expand All @@ -17,7 +18,7 @@ type EC2Resp struct {
}

type S3Object struct {
SizeInBytes int64
SizeInBytes int64
Name, ObjectType, LastModified, Size, StorageClass string
}

Expand Down Expand Up @@ -140,10 +141,15 @@ type EcsClusterResp struct {
}

type EcsServiceResp struct {
ServiceName string
Status string
DesiredCount string
RunningCount string
TaskDefinition string
ServiceArn string
ServiceName string
Status string
DesiredCount string
RunningCount string
TaskDefinition string
ServiceArn string
}

type EcsTaskResp struct {
TaskId string
*ecsTypes.Task
}
152 changes: 78 additions & 74 deletions internal/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,80 +5,84 @@ type ContextKey string

// A collection of context keys.
const (
KeySelectedCloud ContextKey = "seleted_cloud"
KeyFactory ContextKey = "factory"
KeyApp ContextKey = "app"
KeyActiveProfile ContextKey = "active-profile"
KeyActiveRegion ContextKey = "active-region"
KeyActiveProject ContextKey = "active-project"
KeySession ContextKey = "session"
ECSClusterName ContextKey = "ecs_cluster_name"
BucketName ContextKey = "bucket_name"
StorageBucketName ContextKey = "storage_bucket_name"
ObjectName ContextKey = "object_name"
FolderName ContextKey = "folder_name"
KeyAliases ContextKey = "aliases"
UserName ContextKey = "user_name"
GroupName ContextKey = "group_name"
RoleName ContextKey = "role_name"
VpcId ContextKey = "vpc_id"
LowercaseY string = "y"
UppercaseY string = "Y"
LowercaseYes string = "yes"
UppercaseYes string = "YES"
LowercaseN string = "n"
UppercaseN string = "N"
LowercaseNo string = "no"
UppercaseNo string = "NO"
LowercaseEc2 string = "ec2"
UppercaseEc2 string = "Ec2"
LowercaseEcsCluster string = "ecs:c"
LowercaseEcsServices string = "ecs:s"
UppercaseEcsCluster string = "ECS:C"
LowercaseS3 string = "s3"
UppercaseS3 string = "S3"
LowercaseEBS string = "ebs"
UppercaseEBS string = "EBS"
LowercaseSg string = "sg"
UppercaseSg string = "SG"
LowercaseIamUser string = "iam:u"
UppercaseIamUser string = "IAM:U"
LowercaseIam string = "iam"
UppercaseIam string = "IAM"
LowercaseIamGroup string = "iam:g"
UppercaseIamGroup string = "IAM:g"
LowercaseIamRole string = "iam:r"
UppercaseIamRole string = "IAM:R"
LowercaseEc2Snapshot string = "ec2:S"
UppercaseEc2Snapshot string = "Ec2:S"
LowercaseEc2Image string = "ec2:i"
UppercaseEc2Image string = "Ec2:I"
LowercaseSQS string = "sqs"
UppercaseSQS string = "SQS"
LowercaseVPC string = "vpc"
UppercaseVPC string = "VPC"
LowercaseSubnet string = "subnet"
UppercaseSubnet string = "SUBNET"
LowercaseLamda string = "lambda"
UppercaseLamda string = "LAMBDA"
LowercaseStorage string = "storage"
UppercaseStorage string = "STORAGE"
Help string = "help"
LowercaseH string = "h"
QuestionMark string = "?"
Quit string = "quit"
LowercaseQ string = "q"
UppercaseQ string = "Q"
QFactorial string = "q!"
Aliases string = "aliases"
Alias string = "alias"
LowercaseA string = "a"
Object string = "OBJ"
StorageObject string = "STORAGE_OBJ"
UserPolicy string = "User Policy"
UserGroupPolicy string = "User Group Policy"
RolePolicy string = "Role Policy"
GroupUsers string = "Group Users"
KeySelectedCloud ContextKey = "seleted_cloud"
KeyFactory ContextKey = "factory"
KeyApp ContextKey = "app"
KeyActiveProfile ContextKey = "active-profile"
KeyActiveRegion ContextKey = "active-region"
KeyActiveProject ContextKey = "active-project"
KeySession ContextKey = "session"
ECSClusterName ContextKey = "ecs_cluster_name"
ECSServiceName ContextKey = "ecs_service_name"
ECSTaskId ContextKey = "ecs_task_id"
BucketName ContextKey = "bucket_name"
StorageBucketName ContextKey = "storage_bucket_name"
ObjectName ContextKey = "object_name"
FolderName ContextKey = "folder_name"
KeyAliases ContextKey = "aliases"
UserName ContextKey = "user_name"
GroupName ContextKey = "group_name"
RoleName ContextKey = "role_name"
VpcId ContextKey = "vpc_id"
LowercaseY string = "y"
UppercaseY string = "Y"
LowercaseYes string = "yes"
UppercaseYes string = "YES"
LowercaseN string = "n"
UppercaseN string = "N"
LowercaseNo string = "no"
UppercaseNo string = "NO"
LowercaseEc2 string = "ec2"
UppercaseEc2 string = "Ec2"
LowercaseEcsCluster string = "ecs:c"
LowercaseEcsServices string = "ecs:s"
LowercaseEcsTasks string = "ecs:t"
LowercaseEcsContainer string = "ecs:cn"
UppercaseEcsCluster string = "ECS:C"
LowercaseS3 string = "s3"
UppercaseS3 string = "S3"
LowercaseEBS string = "ebs"
UppercaseEBS string = "EBS"
LowercaseSg string = "sg"
UppercaseSg string = "SG"
LowercaseIamUser string = "iam:u"
UppercaseIamUser string = "IAM:U"
LowercaseIam string = "iam"
UppercaseIam string = "IAM"
LowercaseIamGroup string = "iam:g"
UppercaseIamGroup string = "IAM:g"
LowercaseIamRole string = "iam:r"
UppercaseIamRole string = "IAM:R"
LowercaseEc2Snapshot string = "ec2:S"
UppercaseEc2Snapshot string = "Ec2:S"
LowercaseEc2Image string = "ec2:i"
UppercaseEc2Image string = "Ec2:I"
LowercaseSQS string = "sqs"
UppercaseSQS string = "SQS"
LowercaseVPC string = "vpc"
UppercaseVPC string = "VPC"
LowercaseSubnet string = "subnet"
UppercaseSubnet string = "SUBNET"
LowercaseLamda string = "lambda"
UppercaseLamda string = "LAMBDA"
LowercaseStorage string = "storage"
UppercaseStorage string = "STORAGE"
Help string = "help"
LowercaseH string = "h"
QuestionMark string = "?"
Quit string = "quit"
LowercaseQ string = "q"
UppercaseQ string = "Q"
QFactorial string = "q!"
Aliases string = "aliases"
Alias string = "alias"
LowercaseA string = "a"
Object string = "OBJ"
StorageObject string = "STORAGE_OBJ"
UserPolicy string = "User Policy"
UserGroupPolicy string = "User Group Policy"
RolePolicy string = "Role Policy"
GroupUsers string = "Group Users"
)

const (
Expand Down
Loading

0 comments on commit 41fe621

Please sign in to comment.