CostExplorer

package
v1.17.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 9, 2024 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AwsxGetTotalCostOfTheRegion = &cobra.Command{
	Use:   "getTotalRegionCostData",
	Short: "Get cost of all cost of a region",
	Long:  `Retrieve the total cost of specified region using AWS Cost Explorer.`,
	Run: func(cmd *cobra.Command, args []string) {
		authFlag, clientAuth, err := authenticate.AuthenticateCommand(cmd)

		if err != nil {
			log.Println("Error during authentication:", err)
			cmd.Help()
			return
		}

		if authFlag {
			regions, err := GetAllRegions(clientAuth)
			if err != nil {
				log.Println("Error fetching regions:", err)
				return
			}

			var costDataResponses []CostDataResponse

			for _, region := range regions {
				costData, err := GetTotalCostOfTheRegion(cmd, clientAuth, region)
				if err != nil {
					log.Println("Error getting cost data for region", region, ":", err)
					continue
				}

				totalCost, services, noofservices := GetformatCostData(costData)
				response := CostDataResponse{
					Region:       region,
					TotalCost:    totalCost,
					Services:     services,
					NoOfServices: noofservices,
				}
				costDataResponses = append(costDataResponses, response)
			}

			responseJSON, err := json.Marshal(costDataResponses)
			if err != nil {
				log.Println("Error marshaling response:", err)
				return
			}
			fmt.Println(string(responseJSON))
		} else {
			log.Println("Authentication failed. Exiting...")
		}
	},
}
View Source
var GetCostDataCmd = &cobra.Command{
	Use:   "get all cost over view",
	Short: "Get cost of all services",
	Long:  `Retrieve the cost for all services using AWS Cost Explorer.`,
	Run: func(cmd *cobra.Command, args []string) {
		authFlag, clientAuth, err := authenticate.AuthenticateCommand(cmd)
		if err != nil {
			log.Println("Error during authentication:", err)
			cmd.Help()
			return
		}

		if authFlag {
			responseType, _ := cmd.Flags().GetString("responseType")
			costData, err := GetCostperService(cmd, clientAuth)
			if err != nil {
				log.Println("Error getting EC2 cost data: ", err)
				return
			}

			if responseType == "frame" {

				fmt.Println(costData)
			} else {

				fmt.Println(costData)
			}
		} else {
			log.Println("Authentication failed. Exiting...")
		}
	},
}

Functions

func GetAllRegions added in v1.15.3

func GetAllRegions(clientAuth *model.Auth) ([]string, error)

func GetCostperService added in v1.15.0

func GetCostperService(cmd *cobra.Command, clientAuth *model.Auth) (string, error)

func GetTotalCostOfTheRegion

func GetTotalCostOfTheRegion(cmd *cobra.Command, clientAuth *model.Auth, region string) (*costexplorer.GetCostAndUsageOutput, error)

func GetformatCostData added in v1.15.3

func GetformatCostData(result *costexplorer.GetCostAndUsageOutput) (string, string, int)

Types

type CostDataResponse added in v1.15.3

type CostDataResponse struct {
	Region       string `json:"region"`
	TotalCost    string `json:"total_cost"`
	Services     string `json:"services"`
	NoOfServices int    `json:"no_of_services"`
}

type CostDetails added in v1.15.2

type CostDetails struct {
	ServiceName   string `json:"service_name"`
	AmortizedCost string `json:"amortized_cost"`
	Start         string `json:"start"`
	End           string `json:"end"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL