Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COST-4945] Create a new API to return list of EC2-compute instances #5117

Merged
merged 153 commits into from
Jul 19, 2024

Conversation

bacciotti
Copy link
Contributor

@bacciotti bacciotti commented May 20, 2024

Jira Ticket

COST-4945

Description

This change creates a new API to return a list of AWS EC2 compute instances.

Testing

  1. Checkout Branch

  2. Restart Koku

  3. Load test data for AWS

    make create-test-customer
    make load-test-customer-data test_source=aws
    
  4. Hit endpoint

    http://127.0.0.1:8000/api/cost-management/v1/reports/aws/resources/ec2-compute/
    
    • You should see a list of EC2 instances in the response data, similar to this:

      {
          "meta": {
              "count": 5,
              "limit": 3,
              "offset": 0,
              "currency": "USD",
              "cost_type": "unblended_cost",
              "filter": {
                  "time_scope_value": "-1",
                  "time_scope_units": "month",
                  "resolution": "monthly"
              },
              "group_by": {},
              "order_by": {},
              "exclude": {},
              "total": {
                  "usage": {
                      "value": 1380.0,
                      "units": "Hrs"
                  },
                  "infrastructure": {
                      "raw": {
                          "value": 132.48,
                          "units": "USD"
                      },
                      "markup": {
                          "value": 13.248,
                          "units": "USD"
                      },
                      "usage": {
                          "value": 0.0,
                          "units": "USD"
                      },
                      "total": {
                          "value": 145.728,
                          "units": "USD"
                      }
                  },
                  "supplementary": {
                      "raw": {
                          "value": 0.0,
                          "units": "USD"
                      },
                      "markup": {
                          "value": 0.0,
                          "units": "USD"
                      },
                      "usage": {
                          "value": 0.0,
                          "units": "USD"
                      },
                      "total": {
                          "value": 0.0,
                          "units": "USD"
                      }
                  },
                  "cost": {
                      "raw": {
                          "value": 132.48,
                          "units": "USD"
                      },
                      "markup": {
                          "value": 13.248,
                          "units": "USD"
                      },
                      "usage": {
                          "value": 0.0,
                          "units": "USD"
                      },
                      "total": {
                          "value": 145.728,
                          "units": "USD"
                      }
                  }
              }
          },
          "links": {
              "first": "/api/cost-management/v1/reports/aws/resources/ec2-compute/?limit=3&offset=0",
              "next": "/api/cost-management/v1/reports/aws/resources/ec2-compute/?limit=3&offset=3",
              "previous": null,
              "last": "/api/cost-management/v1/reports/aws/resources/ec2-compute/?limit=3&offset=2"
          },
          "data": [
              {
                  "date": "2024-07",
                  "resource_ids": [
                      {
                          "resource_id": "i-55555557",
                          "values": [
                              {
                                  "resource_id": "i-55555557",
                                  "date": "2024-07",
                                  "usage": {
                                      "value": 276.0,
                                      "units": "Hrs"
                                  },
                                  "source_uuid": [
                                      "cfbf64c7-3d58-43fd-a18f-4cd217e6a2d4"
                                  ],
                                  "account_alias": null,
                                  "account": "9999999999998",
                                  "instance_name": "instance_name_3",
                                  "instance_type": "m5.large",
                                  "operating_system": "Debian",
                                  "region": "us-east-1",
                                  "vcpu": 2,
                                  "memory": "8 GiB",
                                  "tags": [
                                      {
                                          "key": "Name",
                                          "values": [
                                              "instance_name_3"
                                          ]
                                      },
                                      {
                                          "key": "version",
                                          "values": [
                                              "gamma"
                                          ]
                                      },
                                      {
                                          "key": "com_redhat_rhel_sla",
                                          "values": [
                                              "Standard"
                                          ]
                                      }
                                  ],
                                  "infrastructure": {
                                      "raw": {
                                          "value": 26.496,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 2.6496,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 29.1456,
                                          "units": "USD"
                                      }
                                  },
                                  "supplementary": {
                                      "raw": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 0.0,
                                          "units": "USD"
                                      }
                                  },
                                  "cost": {
                                      "raw": {
                                          "value": 26.496,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 2.6496,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 29.1456,
                                          "units": "USD"
                                      }
                                  }
                              }
                          ]
                      },
                      {
                          "resource_id": "i-55555556",
                          "values": [
                              {
                                  "resource_id": "i-55555556",
                                  "date": "2024-07",
                                  "usage": {
                                      "value": 276.0,
                                      "units": "Hrs"
                                  },
                                  "source_uuid": [
                                      "cfbf64c7-3d58-43fd-a18f-4cd217e6a2d4"
                                  ],
                                  "account_alias": null,
                                  "account": "9999999999998",
                                  "instance_name": "instance-name-2",
                                  "instance_type": "m5.large",
                                  "operating_system": "Gentoo Linux",
                                  "region": "us-east-1",
                                  "vcpu": 2,
                                  "memory": "8 GiB",
                                  "tags": [
                                      {
                                          "key": "Name",
                                          "values": [
                                              "instance-name-2"
                                          ]
                                      },
                                      {
                                          "key": "version",
                                          "values": [
                                              "beta"
                                          ]
                                      },
                                      {
                                          "key": "CoM_RedHat_Rhel_varianT",
                                          "values": [
                                              "Workstation"
                                          ]
                                      },
                                      {
                                          "key": "com_redhat_rhel_conversion",
                                          "values": [
                                              "True"
                                          ]
                                      }
                                  ],
                                  "infrastructure": {
                                      "raw": {
                                          "value": 26.496,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 2.6496,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 29.1456,
                                          "units": "USD"
                                      }
                                  },
                                  "supplementary": {
                                      "raw": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 0.0,
                                          "units": "USD"
                                      }
                                  },
                                  "cost": {
                                      "raw": {
                                          "value": 26.496,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 2.6496,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 29.1456,
                                          "units": "USD"
                                      }
                                  }
                              }
                          ]
                      },
                      {
                          "resource_id": "i-55555555",
                          "values": [
                              {
                                  "resource_id": "i-55555555",
                                  "date": "2024-07",
                                  "usage": {
                                      "value": 276.0,
                                      "units": "Hrs"
                                  },
                                  "source_uuid": [
                                      "cfbf64c7-3d58-43fd-a18f-4cd217e6a2d4"
                                  ],
                                  "account_alias": null,
                                  "account": "9999999999998",
                                  "instance_name": "instance-name-1",
                                  "instance_type": "m5.large",
                                  "operating_system": "Fedora",
                                  "region": "us-east-1",
                                  "vcpu": 2,
                                  "memory": "8 GiB",
                                  "tags": [
                                      {
                                          "key": "Name",
                                          "values": [
                                              "instance-name-1"
                                          ]
                                      },
                                      {
                                          "key": "version",
                                          "values": [
                                              "prod"
                                          ]
                                      },
                                      {
                                          "key": "dashed-key-on-aws",
                                          "values": [
                                              "dashed-value"
                                          ]
                                      }
                                  ],
                                  "infrastructure": {
                                      "raw": {
                                          "value": 26.496,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 2.6496,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 29.1456,
                                          "units": "USD"
                                      }
                                  },
                                  "supplementary": {
                                      "raw": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 0.0,
                                          "units": "USD"
                                      }
                                  },
                                  "cost": {
                                      "raw": {
                                          "value": 26.496,
                                          "units": "USD"
                                      },
                                      "markup": {
                                          "value": 2.6496,
                                          "units": "USD"
                                      },
                                      "usage": {
                                          "value": 0.0,
                                          "units": "USD"
                                      },
                                      "total": {
                                          "value": 29.1456,
                                          "units": "USD"
                                      }
                                  }
                              }
                          ]
                      }
                  ]
              }
          ]
      }
      
    • Example valid filter/order by params to use with this API

      filter[time_scope_value]=-2
      filter[time_scope_units]=month
      filter[resolution]=monthly
      filter[region]=us-east-1
      filter[account]=*
      order_by[usage]=asc
      order_by[cost]=desc
      filter[tag:version]=beta
      filter[operating_system]=linux
      filter[instance_name]=instance
      
    • Example invalid params not expected to work with this API

      filter[time_scope_value]=-10
      filter[time_scope_units]=day
      filter[resolution]=daily
      filter[tags]=beta
      filter[bad_param]=bad-param-value
      filter[vcpu]=1
      filter[limit]=3
      filter[offset]=1
      order_by[bad_param]=asc
      start_date=2024-07-01
      end_date=2024-07-1
      

Release Notes

  • proposed release note
* [COST-4945](https://issues.redhat.com/browse/COST-4945) Create a new API to return a list of AWS EC2 compute instances.

@bacciotti bacciotti added the aws-smoke-tests pr_check will build the image and run aws + ocp on aws smoke tests label May 20, 2024
Copy link

codecov bot commented May 20, 2024

Codecov Report

Attention: Patch coverage is 92.10526% with 9 lines in your changes missing coverage. Please review.

Project coverage is 94.1%. Comparing base (7c64c4f) to head (45decf9).

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #5117   +/-   ##
=====================================
  Coverage   94.1%   94.1%           
=====================================
  Files        373     373           
  Lines      31040   31127   +87     
  Branches    4564    4571    +7     
=====================================
+ Hits       29214   29297   +83     
- Misses      1162    1165    +3     
- Partials     664     665    +1     

@cgoodfred cgoodfred removed the aws-smoke-tests pr_check will build the image and run aws + ocp on aws smoke tests label May 28, 2024
@djnakabaale djnakabaale added smoke-tests pr_check will build the image and run minimal required smokes and removed aws-smoke-tests pr_check will build the image and run aws + ocp on aws smoke tests labels Jul 11, 2024
@djnakabaale djnakabaale marked this pull request as ready for review July 16, 2024 01:52
@djnakabaale djnakabaale requested review from a team as code owners July 16, 2024 01:52
Copy link
Member

@maskarb maskarb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few comments:

koku/api/query_params.py Outdated Show resolved Hide resolved
koku/api/report/aws/serializers.py Outdated Show resolved Hide resolved
koku/api/report/aws/serializers.py Show resolved Hide resolved
koku/api/report/aws/provider_map.py Outdated Show resolved Hide resolved
koku/api/report/aws/serializers.py Show resolved Hide resolved
koku/api/report/aws/query_handler.py Outdated Show resolved Hide resolved
koku/api/report/view.py Outdated Show resolved Hide resolved
- move report_specific filters to main filter map
- use report_type instead of kwargs in get_paginator
- do not use deepcopy - just overwrite query_data
- resolution and time_scope_units are always monthly and month respectively
- overide start and end date params in base ParamSerializer
- overide limit and offset in base FilterSerializer
Copy link
Member

@maskarb maskarb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Nicely done.

@djnakabaale djnakabaale enabled auto-merge (squash) July 19, 2024 10:20
@djnakabaale djnakabaale merged commit 45d8e31 into main Jul 19, 2024
11 checks passed
@djnakabaale djnakabaale deleted the cost-4945_awsec2_compute_api branch July 19, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
smoke-tests pr_check will build the image and run minimal required smokes smokes-required
Projects
None yet
8 participants