How to Get AWS Workspace details including the Operating System using AWS SDK for Go

133 Views Asked by At

I have a script to get all of the AWS workspaces in a specific account and region and list the details as well then output it to a json file. I am not able to get the Operating system details when using the function DescribeWorkspaces. Here's the error I'm getting:

./workspaces.go:55:43: workspace.OperatingSystem undefined (type "github.com/aws/aws-sdk-go-v2/service/workspaces/types".Workspace has no field or method OperatingSystem)

Here's my code:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "os"

    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/service/sts"
    "github.com/aws/aws-sdk-go-v2/service/workspaces"
)

func main() {
    // Specify the AWS region you want to query
    region := "us-east-1" // Change to your desired region

    // Create an AWS SDK configuration with your credentials
    cfg, err := config.LoadDefaultConfig(context.TODO())
    if err != nil {
        fmt.Println("Error loading AWS config:", err)
        return
    }

    // Create an STS client to get the account ID
    stsClient := sts.NewFromConfig(cfg)
    accountID, err := getAWSAccountID(stsClient)
    if err != nil {
        fmt.Println("Error getting AWS account ID:", err)
        return
    }

    // Create a Workspaces client
    client := workspaces.NewFromConfig(cfg)

    // List Workspaces instances in the specified region
    resp, err := client.DescribeWorkspaces(context.TODO(), &workspaces.DescribeWorkspacesInput{})
    if err != nil {
        fmt.Println("Error describing Workspaces instances:", err)
        return
    }

    // Create a struct to store the details, including account, region, and tags
    var workspaceDetails []map[string]interface{}

    // Iterate through the Workspaces instances and store details
    for _, workspace := range resp.Workspaces {
        // Extract workspace details
        details := map[string]interface{}{
            "AccountID":      accountID,
            "Region":         region,
            "WorkspaceID":    *workspace.WorkspaceId,
            "BundleID":       *workspace.BundleId,
            "UserName":       *workspace.UserName,
            "OperatingSystem": *workspace.OperatingSystem,
            // Include other fields as needed
        }
        workspaceDetails = append(workspaceDetails, details)
    }

    // Create a JSON file to store the details
    outputFile := "workspaces.json"
    file, err := os.Create(outputFile)
    if err != nil {
        fmt.Println("Error creating JSON file:", err)
        return
    }
    defer file.Close()

    // Encode the workspace details and write to the JSON file
    encoder := json.NewEncoder(file)
    encoder.SetIndent("", "  ")
    if err := encoder.Encode(workspaceDetails); err != nil {
        fmt.Println("Error encoding JSON:", err)
        return
    }

    fmt.Printf("Workspace details have been saved to %s\n", outputFile)
}

func getAWSAccountID(stsClient *sts.Client) (string, error) {
    resp, err := stsClient.GetCallerIdentity(context.TODO(), &sts.GetCallerIdentityInput{})
    if err != nil {
        return "", err
    }
    return *resp.Account, nil
}

I understand that there might not be an OperatingSystem in the Workspace type, thus the error. But what can I do to be able to get the Operating System details (either Linux or Windows) per Workspace and append it to the output json file?

Expected output should be something like this:

[
  {
    "Workspace": {
      "BundleId": "wsb-sjklao909", 
      "ComputerName": "A-3HKHAKSHW", 
      "DirectoryId": "d-8237229", 
      "ErrorCode": null,
      "ErrorMessage": null,
      "IpAddress": "1sampleIP", 
      "ModificationStates": [],
      "RelatedWorkspaces": null,
      "RootVolumeEncryptionEnabled": null,
      "State": "STOPPED", 
      "SubnetId": "subnet-sample", 
      "UserName": "username", 
      "UserVolumeEncryptionEnabled": null,
      "VolumeEncryptionKey": null,
      "WorkspaceId": "ws-d77896y", 
      "OperatingSystem": "LINUX or WINDOWS"
      "WorkspaceProperties": {
        "ComputeTypeName": "STANDARD",
        "Protocols": [
          "PCOIP"
        ],
        "RootVolumeSizeGib": 80,
        "RunningMode": "AUTO_STOP",
        "RunningModeAutoStopTimeoutInMinutes": 60,
        "UserVolumeSizeGib": 50
      }
    },
    "Account": "AWSAccount",
    "Region": "us-east-1"
  }
]
0

There are 0 best solutions below