Skip to content

Commit

Permalink
🐛 fix aws ecs cluster tasks resource
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey committed Jun 5, 2024
1 parent ef14521 commit cf6aea5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 76 deletions.
2 changes: 1 addition & 1 deletion providers/aws/resources/aws.lr
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ private aws.ecs.task {
// User-defined tags associated with the ECS task
tags map[string]string
// List of AWS ECS containers
containers []aws.ecs.container
containers() []aws.ecs.container
}

// Amazon ECS container
Expand Down
18 changes: 15 additions & 3 deletions providers/aws/resources/aws.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 53 additions & 72 deletions providers/aws/resources/aws_ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (a *mqlAwsEcsCluster) tasks() ([]interface{}, error) {
params.NextToken = nextToken
}
for _, task := range resp.TaskArns {
mqlTask, err := CreateResource(a.MqlRuntime, "aws.ecs.task",
mqlTask, err := NewResource(a.MqlRuntime, "aws.ecs.task",
map[string]*llx.RawData{
"arn": llx.StringData(task),
"clusterName": llx.StringData(name),
Expand Down Expand Up @@ -348,7 +348,7 @@ func initAwsEcsTask(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[
}
svc := conn.Ecs(region)
ctx := context.Background()
params := &ecs.DescribeTasksInput{Tasks: []string{a}}
params := &ecs.DescribeTasksInput{Tasks: []string{a}, Cluster: &clusterName}
params.Cluster = &clusterName
taskDetails, err := svc.DescribeTasks(ctx, params)
if err != nil {
Expand All @@ -357,106 +357,87 @@ func initAwsEcsTask(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[
if len(taskDetails.Tasks) != 1 {
return nil, nil, errors.Newf("only expected one task, got %d", len(taskDetails.Tasks))
}
t := taskDetails.Tasks[0]
taskDefinitionArn := t.TaskDefinitionArn
definition, err := svc.DescribeTaskDefinition(ctx, &ecs.DescribeTaskDefinitionInput{TaskDefinition: taskDefinitionArn})
if err != nil {
return nil, nil, err
}
containerLogDriverMap := make(map[string]string)
containerCommandMap := make(map[string][]string)

for i := range definition.TaskDefinition.ContainerDefinitions {
cd := definition.TaskDefinition.ContainerDefinitions[i]
if cd.Name != nil {
containerCommandMap[*cd.Name] = cd.Command
if cd.LogConfiguration != nil {
containerLogDriverMap[*cd.Name] = string(cd.LogConfiguration.LogDriver)
} else {
containerLogDriverMap[*cd.Name] = "none"
}
}
}

t := taskDetails.Tasks[0]
args["clusterName"] = llx.StringData(clusterName)
args["connectivity"] = llx.StringData(string(t.Connectivity))
args["lastStatus"] = llx.StringData(convert.ToString(t.LastStatus))
args["platformFamily"] = llx.StringData(convert.ToString(t.PlatformFamily))
args["platformVersion"] = llx.StringData(convert.ToString(t.PlatformVersion))
args["tags"] = llx.MapData(ecsTags(t.Tags), types.String)
res, err := CreateResource(runtime, "aws.ecs.task", args)
if err != nil {
return args, nil, err
}
res.(*mqlAwsEcsTask).cacheContainers = t.Containers
res.(*mqlAwsEcsTask).region = region
res.(*mqlAwsEcsTask).attachments = t.Attachments
res.(*mqlAwsEcsTask).clusterName = clusterName

containers := []interface{}{}
// pf, _ := a.platformFamily()
// pv, _ := a.PlatformVersion()
return args, res, nil
}

type mqlAwsEcsTaskInternal struct {
cacheContainers []ecstypes.Container
region string
attachments []ecstypes.Attachment
clusterName string
}

for _, c := range t.Containers {
func (t *mqlAwsEcsTask) containers() ([]interface{}, error) {
conn := t.MqlRuntime.Connection.(*connection.AwsConnection)
ctx := context.Background()

containers := []interface{}{}
for _, c := range t.cacheContainers {
containerLogDriverMap := make(map[string]string)
containerCommandMap := make(map[string]string)
cmds := []interface{}{}
for i := range containerCommandMap[convert.ToString(c.Name)] {
cmds = append(cmds, containerCommandMap[convert.ToString(c.Name)][i])
}
publicIp := getContainerIP(ctx, conn, t.Attachments, c, region)
publicIp := getContainerIP(ctx, conn, t.attachments, c, t.region)
name := convert.ToString(c.Name)
if publicIp != "" {
name = name + "-" + publicIp
}

mqlContainer, err := CreateResource(runtime, "aws.ecs.container",
// for i := range definition.TaskDefinition.ContainerDefinitions {
// cd := definition.TaskDefinition.ContainerDefinitions[i]
// if cd.Name != nil {
// containerCommandMap[*cd.Name] = cd.Command
// if cd.LogConfiguration != nil {
// containerLogDriverMap[*cd.Name] = string(cd.LogConfiguration.LogDriver)
// } else {
// containerLogDriverMap[*cd.Name] = "none"
// }
// }
// }

mqlContainer, err := CreateResource(t.MqlRuntime, "aws.ecs.container",
map[string]*llx.RawData{
"name": llx.StringData(name),
// "platformFamily", pf,
// "platformVersion", pv,
"arn": llx.StringData(convert.ToString(c.ContainerArn)),
"name": llx.StringDataPtr(c.Name),
"status": llx.StringData(convert.ToString(c.LastStatus)),
"publicIp": llx.StringData(publicIp),
"arn": llx.StringData(convert.ToString(c.ContainerArn)),
"logDriver": llx.StringData(containerLogDriverMap[convert.ToString(c.Name)]),
"image": llx.StringData(convert.ToString(c.Image)),
"clusterName": llx.StringData(clusterName),
"taskDefinitionArn": llx.StringData(convert.ToString(taskDefinitionArn)),
"region": llx.StringData(region),
"clusterName": llx.StringData(t.clusterName),
"taskDefinitionArn": llx.StringData("test"),
"region": llx.StringData(t.region),
"command": llx.ArrayData(cmds, types.Any),
"taskArn": llx.StringData(convert.ToString(t.TaskArn)),
"taskArn": llx.StringData(t.Arn.Data),
"runtimeId": llx.StringData(convert.ToString(c.RuntimeId)),
"containerName": llx.StringData(convert.ToString(c.Name)),
"platformFamily": llx.StringData(t.PlatformFamily.Data),
"platformVersion": llx.StringData(t.PlatformVersion.Data),
})
if err != nil {
return args, nil, err
return nil, err
}
containers = append(containers, mqlContainer)
}
args["containers"] = llx.ArrayData(containers, types.Resource("aws.ecs.container"))

return args, nil, nil
}

func initAwsEcsContainer(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
if len(args) > 2 {
return args, nil, nil
}
if len(args) == 0 {
if ids := getAssetIdentifier(runtime); ids != nil {
args["arn"] = llx.StringData(ids.arn)
}
}
obj, err := CreateResource(runtime, "aws.ecs", map[string]*llx.RawData{})
if err != nil {
return nil, nil, err
}
ecs := obj.(*mqlAwsEcs)

rawResources := ecs.GetContainers()

if rawResources.Error != nil {
return nil, nil, rawResources.Error
}

arnVal := args["arn"].Value.(string)
for i := range rawResources.Data {
container := rawResources.Data[i].(*mqlAwsEcsContainer)
if container.Arn.Data == arnVal {
return args, container, nil
}
}

return nil, nil, errors.New("container does not exist")
return containers, nil
}

func getContainerIP(ctx context.Context, conn *connection.AwsConnection, attachments []ecstypes.Attachment, c ecstypes.Container, region string) string {
Expand Down

0 comments on commit cf6aea5

Please sign in to comment.