|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + lm "github.com/aws/aws-sdk-go-v2/service/licensemanager" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/licensemanager/types" |
| 10 | + "github.com/aws/aws-sdk-go-v2/service/sts" |
| 11 | +) |
| 12 | + |
| 13 | +const timeFormat = time.RFC3339 |
| 14 | + |
| 15 | +type mockLicenseManagerClient struct { |
| 16 | + licenses map[string]types.GrantedLicense |
| 17 | + checkedOutLicenses map[string]licenseInfo |
| 18 | + licenseCounter int |
| 19 | +} |
| 20 | + |
| 21 | +type mockSTSClient struct { |
| 22 | + accountNumber string |
| 23 | +} |
| 24 | + |
| 25 | +type licenseInfo struct { |
| 26 | + checkOutInput lm.CheckoutLicenseInput |
| 27 | + expiryTime time.Time |
| 28 | +} |
| 29 | + |
| 30 | +func (m *mockLicenseManagerClient) AddLicenseForSku(productSku string, accountNumber string, includeSkuInReturn bool) { |
| 31 | + if m.licenses == nil { |
| 32 | + m.licenses = map[string]types.GrantedLicense{} |
| 33 | + } |
| 34 | + licenseArn := fmt.Sprintf("arn:aws:license-manager::%s:license:l-%06d", accountNumber, m.licenseCounter) |
| 35 | + m.licenseCounter++ |
| 36 | + license := types.GrantedLicense{ |
| 37 | + LicenseArn: &licenseArn, |
| 38 | + } |
| 39 | + if includeSkuInReturn { |
| 40 | + license.ProductSKU = &productSku |
| 41 | + } |
| 42 | + m.licenses[productSku] = license |
| 43 | +} |
| 44 | + |
| 45 | +func (m *mockLicenseManagerClient) Clear() { |
| 46 | + m.licenses = map[string]types.GrantedLicense{} |
| 47 | + m.checkedOutLicenses = map[string]licenseInfo{} |
| 48 | + m.licenseCounter = 0 |
| 49 | +} |
| 50 | + |
| 51 | +func (m *mockLicenseManagerClient) ListReceivedLicenses(ctx context.Context, params *lm.ListReceivedLicensesInput, optFns ...func(*lm.Options)) (*lm.ListReceivedLicensesOutput, error) { |
| 52 | + var productIDs []string |
| 53 | + for _, filter := range params.Filters { |
| 54 | + if *filter.Name == productSKUField { |
| 55 | + productIDs = filter.Values |
| 56 | + } |
| 57 | + } |
| 58 | + var licenses []types.GrantedLicense |
| 59 | + for _, productID := range productIDs { |
| 60 | + if license, ok := m.licenses[productID]; ok { |
| 61 | + licenses = append(licenses, license) |
| 62 | + } |
| 63 | + } |
| 64 | + return &lm.ListReceivedLicensesOutput{ |
| 65 | + Licenses: licenses, |
| 66 | + }, nil |
| 67 | +} |
| 68 | +func (m *mockLicenseManagerClient) CheckoutLicense(ctx context.Context, params *lm.CheckoutLicenseInput, optFns ...func(*lm.Options)) (*lm.CheckoutLicenseOutput, error) { |
| 69 | + consumptionToken := params.ClientToken |
| 70 | + if consumptionToken == nil { |
| 71 | + return nil, fmt.Errorf("unable to checkout license, no consumption token provided") |
| 72 | + } |
| 73 | + expiryTime := time.Now().Add(time.Hour * 24) |
| 74 | + expiryTS := expiryTime.Format(timeFormat) |
| 75 | + m.checkedOutLicenses[*consumptionToken] = licenseInfo{ |
| 76 | + checkOutInput: *params, |
| 77 | + expiryTime: expiryTime, |
| 78 | + } |
| 79 | + return &lm.CheckoutLicenseOutput{ |
| 80 | + LicenseConsumptionToken: consumptionToken, |
| 81 | + Expiration: &expiryTS, |
| 82 | + }, nil |
| 83 | +} |
| 84 | +func (m *mockLicenseManagerClient) CheckInLicense(ctx context.Context, params *lm.CheckInLicenseInput, optFns ...func(*lm.Options)) (*lm.CheckInLicenseOutput, error) { |
| 85 | + if params.LicenseConsumptionToken == nil { |
| 86 | + return nil, fmt.Errorf("can't check in license without consumption token") |
| 87 | + } |
| 88 | + if _, ok := m.checkedOutLicenses[*params.LicenseConsumptionToken]; ok { |
| 89 | + return nil, fmt.Errorf("no license checked in for consumption token") |
| 90 | + } |
| 91 | + delete(m.checkedOutLicenses, *params.LicenseConsumptionToken) |
| 92 | + return nil, nil |
| 93 | +} |
| 94 | +func (m *mockLicenseManagerClient) ExtendLicenseConsumption(ctx context.Context, params *lm.ExtendLicenseConsumptionInput, optFns ...func(*lm.Options)) (*lm.ExtendLicenseConsumptionOutput, error) { |
| 95 | + token := params.LicenseConsumptionToken |
| 96 | + if token == nil { |
| 97 | + return nil, fmt.Errorf("no token provided, cannot extend checkout") |
| 98 | + } |
| 99 | + if _, ok := m.checkedOutLicenses[*token]; ok { |
| 100 | + return nil, fmt.Errorf("no license checked in for consumption token") |
| 101 | + } |
| 102 | + return nil, nil |
| 103 | +} |
| 104 | +func (m *mockLicenseManagerClient) GetLicenseUsage(ctx context.Context, params *lm.GetLicenseUsageInput, optFns ...func(*lm.Options)) (*lm.GetLicenseUsageOutput, error) { |
| 105 | + licenseArn := params.LicenseArn |
| 106 | + if licenseArn == nil { |
| 107 | + return nil, fmt.Errorf("license arn is missing but is required") |
| 108 | + } |
| 109 | + var entitlementUsage []types.EntitlementUsage |
| 110 | + for _, value := range m.checkedOutLicenses { |
| 111 | + // find only the check-outs for this license |
| 112 | + if license, ok := m.licenses[*value.checkOutInput.ProductSKU]; ok { |
| 113 | + if *license.LicenseArn == *licenseArn { |
| 114 | + // add each usage separately, no need to combine into one |
| 115 | + for _, data := range value.checkOutInput.Entitlements { |
| 116 | + entitlementUsage = append(entitlementUsage, types.EntitlementUsage{Name: data.Name, ConsumedValue: data.Value, Unit: data.Unit}) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return &lm.GetLicenseUsageOutput{ |
| 122 | + LicenseUsage: &types.LicenseUsage{EntitlementUsages: entitlementUsage}}, nil |
| 123 | +} |
| 124 | + |
| 125 | +func (m *mockSTSClient) GetCallerIdentity(ctx context.Context, params *sts.GetCallerIdentityInput, optFns ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error) { |
| 126 | + return &sts.GetCallerIdentityOutput{Account: &m.accountNumber}, nil |
| 127 | +} |
0 commit comments