diff --git a/pkg/health/health.go b/pkg/health/health.go index 904702f..bae6844 100644 --- a/pkg/health/health.go +++ b/pkg/health/health.go @@ -24,19 +24,20 @@ const ( // Indicates that resource is missing in the cluster. HealthStatusMissing HealthStatusCode = "Missing" - HealthStatusCreating = "Creating" - HealthStatusDeleted = "Deleted" - HealthStatusDeleting = "Deleting" - HealthStatusError = "Error" - HealthStatusInaccesible = "Inaccesible" - HealthStatusInfo = "Info" - HealthStatusPending = "Pending" - HealthStatusMaintenance = "Maintenance" - HealthStatusScaling = "Scaling" - HealthStatusUnhealthy = "Unhealthy" - HealthStatusUpdating = "Updating" - HealthStatusWarning = "Warning" - HealthStatusStopping = "Stopping" + HealthStatusCreating HealthStatusCode = "Creating" + HealthStatusDeleted HealthStatusCode = "Deleted" + HealthStatusDeleting HealthStatusCode = "Deleting" + HealthStatusError HealthStatusCode = "Error" + HealthStatusInaccesible HealthStatusCode = "Inaccesible" + HealthStatusInfo HealthStatusCode = "Info" + HealthStatusPending HealthStatusCode = "Pending" + HealthStatusMaintenance HealthStatusCode = "Maintenance" + HealthStatusScaling HealthStatusCode = "Scaling" + HealthStatusUnhealthy HealthStatusCode = "Unhealthy" + HealthStatusUpdating HealthStatusCode = "Updating" + HealthStatusWarning HealthStatusCode = "Warning" + HealthStatusStopped HealthStatusCode = "Stopped" + HealthStatusStopping HealthStatusCode = "Stopping" ) // Implements custom health assessment that overrides built-in assessment diff --git a/pkg/health/health_aws.go b/pkg/health/health_aws.go index ddc92b4..8ecc01f 100644 --- a/pkg/health/health_aws.go +++ b/pkg/health/health_aws.go @@ -2,77 +2,104 @@ package health import "strings" +const ( + AWSResourceTypeEBS string = "ebs" + AWSResourceTypeEC2 string = "ec2" + AWSResourceTypeEKS string = "eks" + AWSResourceTypeELB string = "elb" + AWSResourceTypeRDS string = "rds" + AWSResourceTypeVPC string = "vpc" + AWSResourceTypeSubnet string = "subnet" +) + // MapAWSStatus maps an AWS resource's statuses to a Health Code -func MapAWSStatus(status string) string { - if s, found := awsStatusMap[strings.ToLower(status)]; found { - return string(s) +func MapAWSStatus(status string, resourceType string) string { + if resourceStatuses, found := awsStatusMap[resourceType]; found { + if v, found := resourceStatuses[strings.ToLower(status)]; found { + return string(v) + } } return string(HealthStatusUnknown) } -var awsStatusMap = map[string]HealthStatusCode{ - "available": HealthStatusHealthy, - "pending": HealthStatusPending, - "running": HealthStatusHealthy, - "shutting-down": HealthStatusDeleting, - "stopped": HealthStatusSuspended, - "stopping": HealthStatusDeleting, - "terminated": HealthStatusDeleted, +var awsStatusMap = map[string]map[string]HealthStatusCode{ + AWSResourceTypeEC2: { + "pending": HealthStatusPending, + "running": HealthStatusHealthy, + "shutting-down": HealthStatusDeleting, + "stopped": HealthStatusStopped, + "stopping": HealthStatusStopping, + "terminated": HealthStatusDeleted, + }, + + AWSResourceTypeEKS: { + "creating": HealthStatusCreating, + "active": HealthStatusHealthy, + "deleting": HealthStatusDeleting, + "failed": HealthStatusError, + "updating": HealthStatusUpdating, + "pending": HealthStatusPending, + }, + + AWSResourceTypeEBS: { + "creating": HealthStatusCreating, + "available": HealthStatusStopped, + "in-use": HealthStatusHealthy, + "deleting": HealthStatusDeleting, + "deleted": HealthStatusDeleted, + "error": HealthStatusError, + }, - // EKS - "creating": HealthStatusCreating, - "active": HealthStatusHealthy, - "deleting": HealthStatusDeleting, - "failed": HealthStatusError, - "updating": HealthStatusUpdating, - // "pending": HealthStatusPending, + AWSResourceTypeRDS: { + "available": HealthStatusHealthy, + "billed": HealthStatusHealthy, + "backing-up": HealthStatusMaintenance, + "configuring-enhanced-monitoring": HealthStatusMaintenance, + "configuring-iam-database-auth": HealthStatusMaintenance, + "configuring-log-exports": HealthStatusMaintenance, + "converting-to-vpc": HealthStatusUpdating, + "creating": HealthStatusCreating, + "delete-precheck": HealthStatusMaintenance, + "deleting": HealthStatusDeleting, + "failed": HealthStatusUnhealthy, + "inaccessible-encryption-credentials": HealthStatusInaccesible, + "inaccessible-encryption-credentials-recoverable": HealthStatusInaccesible, + "incompatible-network": HealthStatusUnhealthy, + "incompatible-option-group": HealthStatusUnhealthy, + "incompatible-parameters": HealthStatusUnhealthy, + "incompatible-restore": HealthStatusUnhealthy, + "insufficient-capacity": HealthStatusUnhealthy, + "maintenance": HealthStatusMaintenance, + "modifying": HealthStatusUpdating, + "moving-to-vpc": HealthStatusMaintenance, + "rebooting": HealthStatusStopping, + "resetting-master-credentials": HealthStatusMaintenance, + "renaming": HealthStatusMaintenance, + "restore-error": HealthStatusError, + "starting": HealthStatusProgressing, + "stopped": HealthStatusStopped, + "stopping": HealthStatusStopping, + "storage-config-upgrade": HealthStatusUpdating, + "storage-full": HealthStatusUnhealthy, + "storage-optimization": HealthStatusMaintenance, + "upgrading": HealthStatusUpdating, + }, - // EBS - // "creating": HealthStatusCreating, - // "available": HealthStatusUpdating, - "in-use": HealthStatusHealthy, - // "deleting": HealthStatusDeleting, - "deleted": HealthStatusDeleted, - "error": HealthStatusError, + AWSResourceTypeELB: { + "active": HealthStatusHealthy, + "provisioning": HealthStatusProgressing, + "active_impaired": HealthStatusWarning, + "failed": HealthStatusError, + }, - // RDS Status - // "available": HealthStatusHealthy, - "billed": HealthStatusHealthy, - "backing-up": HealthStatusMaintenance, - "configuring-enhanced-monitoring": HealthStatusMaintenance, - "configuring-iam-database-auth": HealthStatusMaintenance, - "configuring-log-exports": HealthStatusMaintenance, - "converting-to-vpc": HealthStatusUpdating, - // "creating": HealthStatusCreating, - "delete-precheck": HealthStatusMaintenance, - // "deleting": HealthStatusDeleting, - // "failed": HealthStatusUnhealthy, - "inaccessible-encryption-credentials": HealthStatusInaccesible, - "inaccessible-encryption-credentials-recoverable": HealthStatusInaccesible, - "incompatible-network": HealthStatusUnhealthy, - "incompatible-option-group": HealthStatusUnhealthy, - "incompatible-parameters": HealthStatusUnhealthy, - "incompatible-restore": HealthStatusUnhealthy, - "insufficient-capacity": HealthStatusUnhealthy, - "maintenance": HealthStatusMaintenance, - "modifying": HealthStatusUpdating, - "moving-to-vpc": HealthStatusMaintenance, - "rebooting": HealthStatusStopping, - "resetting-master-credentials": HealthStatusMaintenance, - "renaming": HealthStatusMaintenance, - "restore-error": HealthStatusError, - "starting": HealthStatusProgressing, - // "stopped": HealthStatusSuspended, - // "stopping": HealthStatusStopping, - "storage-config-upgrade": HealthStatusUpdating, - "storage-full": HealthStatusUnhealthy, - "storage-optimization": HealthStatusMaintenance, - "upgrading": HealthStatusUpdating, + AWSResourceTypeVPC: { + "pending": HealthStatusPending, + "available": HealthStatusHealthy, + }, - // ELB - // "active": HealthStatusHealthy, - "provisioning": HealthStatusProgressing, - "active_impaired": HealthStatusWarning, - // "failed": HealthStatusError, + AWSResourceTypeSubnet: { + "pending": HealthStatusPending, + "available": HealthStatusHealthy, + }, } diff --git a/pkg/health/health_aws_test.go b/pkg/health/health_aws_test.go new file mode 100644 index 0000000..7821a29 --- /dev/null +++ b/pkg/health/health_aws_test.go @@ -0,0 +1,39 @@ +package health + +import "testing" + +func TestMapAWSStatus(t *testing.T) { + type args struct { + status string + resourceType string + } + tests := []struct { + name string + args args + want HealthStatusCode + }{ + { + name: "ec2", + args: args{status: "shutting-down", resourceType: AWSResourceTypeEC2}, + want: HealthStatusDeleting, + }, + { + name: "unknown resource", + args: args{status: "shutting-down", resourceType: "blob"}, + want: HealthStatusUnknown, + }, + { + name: "unknown type", + args: args{status: "wakingup", resourceType: AWSResourceTypeEC2}, + want: HealthStatusUnknown, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := MapAWSStatus(tt.args.status, tt.args.resourceType); got != string(tt.want) { + t.Errorf("MapAWSStatus() = %v, want %v", got, tt.want) + } + }) + } +}