Skip to content

Commit da51703

Browse files
manukm-ibmpadamstx
andauthoredDec 8, 2023
feat(Usage Reports): add billing snapshot config validate service to usage reports (#291)
Signed-off-by: manu.k.m <manu.k.m@ibm.com> Co-authored-by: Phil Adams <phil_adams@us.ibm.com>
1 parent a0d1bf3 commit da51703

4 files changed

+639
-0
lines changed
 

‎usagereportsv4/usage_reports_v4.go

+221
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,91 @@ func (usageReports *UsageReportsV4) DeleteReportsSnapshotConfigWithContext(ctx c
972972
return
973973
}
974974

975+
// ValidateReportsSnapshotConfig : Verify billing to COS authorization
976+
// Verify billing service to COS bucket authorization for the given account_id. If COS bucket information is not
977+
// provided, COS bucket information is retrieved from the configuration file.
978+
func (usageReports *UsageReportsV4) ValidateReportsSnapshotConfig(validateReportsSnapshotConfigOptions *ValidateReportsSnapshotConfigOptions) (result *SnapshotConfigValidateResponse, response *core.DetailedResponse, err error) {
979+
return usageReports.ValidateReportsSnapshotConfigWithContext(context.Background(), validateReportsSnapshotConfigOptions)
980+
}
981+
982+
// ValidateReportsSnapshotConfigWithContext is an alternate form of the ValidateReportsSnapshotConfig method which supports a Context parameter
983+
func (usageReports *UsageReportsV4) ValidateReportsSnapshotConfigWithContext(ctx context.Context, validateReportsSnapshotConfigOptions *ValidateReportsSnapshotConfigOptions) (result *SnapshotConfigValidateResponse, response *core.DetailedResponse, err error) {
984+
err = core.ValidateNotNil(validateReportsSnapshotConfigOptions, "validateReportsSnapshotConfigOptions cannot be nil")
985+
if err != nil {
986+
return
987+
}
988+
err = core.ValidateStruct(validateReportsSnapshotConfigOptions, "validateReportsSnapshotConfigOptions")
989+
if err != nil {
990+
return
991+
}
992+
993+
builder := core.NewRequestBuilder(core.POST)
994+
builder = builder.WithContext(ctx)
995+
builder.EnableGzipCompression = usageReports.GetEnableGzipCompression()
996+
_, err = builder.ResolveRequestURL(usageReports.Service.Options.URL, `/v1/billing-reports-snapshot-config/validate`, nil)
997+
if err != nil {
998+
return
999+
}
1000+
1001+
for headerName, headerValue := range validateReportsSnapshotConfigOptions.Headers {
1002+
builder.AddHeader(headerName, headerValue)
1003+
}
1004+
1005+
sdkHeaders := common.GetSdkHeaders("usage_reports", "V4", "ValidateReportsSnapshotConfig")
1006+
for headerName, headerValue := range sdkHeaders {
1007+
builder.AddHeader(headerName, headerValue)
1008+
}
1009+
builder.AddHeader("Accept", "application/json")
1010+
builder.AddHeader("Content-Type", "application/json")
1011+
1012+
body := make(map[string]interface{})
1013+
if validateReportsSnapshotConfigOptions.AccountID != nil {
1014+
body["account_id"] = validateReportsSnapshotConfigOptions.AccountID
1015+
}
1016+
if validateReportsSnapshotConfigOptions.Interval != nil {
1017+
body["interval"] = validateReportsSnapshotConfigOptions.Interval
1018+
}
1019+
if validateReportsSnapshotConfigOptions.CosBucket != nil {
1020+
body["cos_bucket"] = validateReportsSnapshotConfigOptions.CosBucket
1021+
}
1022+
if validateReportsSnapshotConfigOptions.CosLocation != nil {
1023+
body["cos_location"] = validateReportsSnapshotConfigOptions.CosLocation
1024+
}
1025+
if validateReportsSnapshotConfigOptions.CosReportsFolder != nil {
1026+
body["cos_reports_folder"] = validateReportsSnapshotConfigOptions.CosReportsFolder
1027+
}
1028+
if validateReportsSnapshotConfigOptions.ReportTypes != nil {
1029+
body["report_types"] = validateReportsSnapshotConfigOptions.ReportTypes
1030+
}
1031+
if validateReportsSnapshotConfigOptions.Versioning != nil {
1032+
body["versioning"] = validateReportsSnapshotConfigOptions.Versioning
1033+
}
1034+
_, err = builder.SetBodyContentJSON(body)
1035+
if err != nil {
1036+
return
1037+
}
1038+
1039+
request, err := builder.Build()
1040+
if err != nil {
1041+
return
1042+
}
1043+
1044+
var rawResponse map[string]json.RawMessage
1045+
response, err = usageReports.Service.Request(request, &rawResponse)
1046+
if err != nil {
1047+
return
1048+
}
1049+
if rawResponse != nil {
1050+
err = core.UnmarshalModel(rawResponse, "", &result, UnmarshalSnapshotConfigValidateResponse)
1051+
if err != nil {
1052+
return
1053+
}
1054+
response.Result = result
1055+
}
1056+
1057+
return
1058+
}
1059+
9751060
// GetReportsSnapshot : Fetch the current or past snapshots
9761061
// Returns the billing reports snapshots captured for the given Account Id in the specific time period.
9771062
func (usageReports *UsageReportsV4) GetReportsSnapshot(getReportsSnapshotOptions *GetReportsSnapshotOptions) (result *SnapshotList, response *core.DetailedResponse, err error) {
@@ -3407,6 +3492,37 @@ func UnmarshalSnapshotConfig(m map[string]json.RawMessage, result interface{}) (
34073492
return
34083493
}
34093494

3495+
// SnapshotConfigValidateResponse : Validated billing service to COS bucket authorization.
3496+
type SnapshotConfigValidateResponse struct {
3497+
// Account ID for which billing report snapshot is configured.
3498+
AccountID *string `json:"account_id,omitempty"`
3499+
3500+
// The name of the COS bucket to store the snapshot of the billing reports.
3501+
CosBucket *string `json:"cos_bucket,omitempty"`
3502+
3503+
// Region of the COS instance.
3504+
CosLocation *string `json:"cos_location,omitempty"`
3505+
}
3506+
3507+
// UnmarshalSnapshotConfigValidateResponse unmarshals an instance of SnapshotConfigValidateResponse from the specified map of raw messages.
3508+
func UnmarshalSnapshotConfigValidateResponse(m map[string]json.RawMessage, result interface{}) (err error) {
3509+
obj := new(SnapshotConfigValidateResponse)
3510+
err = core.UnmarshalPrimitive(m, "account_id", &obj.AccountID)
3511+
if err != nil {
3512+
return
3513+
}
3514+
err = core.UnmarshalPrimitive(m, "cos_bucket", &obj.CosBucket)
3515+
if err != nil {
3516+
return
3517+
}
3518+
err = core.UnmarshalPrimitive(m, "cos_location", &obj.CosLocation)
3519+
if err != nil {
3520+
return
3521+
}
3522+
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj))
3523+
return
3524+
}
3525+
34103526
// Subscription : Subscription struct
34113527
type Subscription struct {
34123528
// The ID of the subscription.
@@ -3700,6 +3816,111 @@ func (options *UpdateReportsSnapshotConfigOptions) SetHeaders(param map[string]s
37003816
return options
37013817
}
37023818

3819+
// ValidateReportsSnapshotConfigOptions : The ValidateReportsSnapshotConfig options.
3820+
type ValidateReportsSnapshotConfigOptions struct {
3821+
// Account ID for which billing report snapshot is configured.
3822+
AccountID *string `json:"account_id" validate:"required"`
3823+
3824+
// Frequency of taking the snapshot of the billing reports.
3825+
Interval *string `json:"interval,omitempty"`
3826+
3827+
// The name of the COS bucket to store the snapshot of the billing reports.
3828+
CosBucket *string `json:"cos_bucket,omitempty"`
3829+
3830+
// Region of the COS instance.
3831+
CosLocation *string `json:"cos_location,omitempty"`
3832+
3833+
// The billing reports root folder to store the billing reports snapshots. Defaults to "IBMCloud-Billing-Reports".
3834+
CosReportsFolder *string `json:"cos_reports_folder,omitempty"`
3835+
3836+
// The type of billing reports to take snapshot of. Possible values are [account_summary, enterprise_summary,
3837+
// account_resource_instance_usage].
3838+
ReportTypes []string `json:"report_types,omitempty"`
3839+
3840+
// A new version of report is created or the existing report version is overwritten with every update. Defaults to
3841+
// "new".
3842+
Versioning *string `json:"versioning,omitempty"`
3843+
3844+
// Allows users to set headers on API requests
3845+
Headers map[string]string
3846+
}
3847+
3848+
// Constants associated with the ValidateReportsSnapshotConfigOptions.Interval property.
3849+
// Frequency of taking the snapshot of the billing reports.
3850+
const (
3851+
ValidateReportsSnapshotConfigOptionsIntervalDailyConst = "daily"
3852+
)
3853+
3854+
// Constants associated with the ValidateReportsSnapshotConfigOptions.ReportTypes property.
3855+
const (
3856+
ValidateReportsSnapshotConfigOptionsReportTypesAccountResourceInstanceUsageConst = "account_resource_instance_usage"
3857+
ValidateReportsSnapshotConfigOptionsReportTypesAccountSummaryConst = "account_summary"
3858+
ValidateReportsSnapshotConfigOptionsReportTypesEnterpriseSummaryConst = "enterprise_summary"
3859+
)
3860+
3861+
// Constants associated with the ValidateReportsSnapshotConfigOptions.Versioning property.
3862+
// A new version of report is created or the existing report version is overwritten with every update. Defaults to
3863+
// "new".
3864+
const (
3865+
ValidateReportsSnapshotConfigOptionsVersioningNewConst = "new"
3866+
ValidateReportsSnapshotConfigOptionsVersioningOverwriteConst = "overwrite"
3867+
)
3868+
3869+
// NewValidateReportsSnapshotConfigOptions : Instantiate ValidateReportsSnapshotConfigOptions
3870+
func (*UsageReportsV4) NewValidateReportsSnapshotConfigOptions(accountID string) *ValidateReportsSnapshotConfigOptions {
3871+
return &ValidateReportsSnapshotConfigOptions{
3872+
AccountID: core.StringPtr(accountID),
3873+
}
3874+
}
3875+
3876+
// SetAccountID : Allow user to set AccountID
3877+
func (_options *ValidateReportsSnapshotConfigOptions) SetAccountID(accountID string) *ValidateReportsSnapshotConfigOptions {
3878+
_options.AccountID = core.StringPtr(accountID)
3879+
return _options
3880+
}
3881+
3882+
// SetInterval : Allow user to set Interval
3883+
func (_options *ValidateReportsSnapshotConfigOptions) SetInterval(interval string) *ValidateReportsSnapshotConfigOptions {
3884+
_options.Interval = core.StringPtr(interval)
3885+
return _options
3886+
}
3887+
3888+
// SetCosBucket : Allow user to set CosBucket
3889+
func (_options *ValidateReportsSnapshotConfigOptions) SetCosBucket(cosBucket string) *ValidateReportsSnapshotConfigOptions {
3890+
_options.CosBucket = core.StringPtr(cosBucket)
3891+
return _options
3892+
}
3893+
3894+
// SetCosLocation : Allow user to set CosLocation
3895+
func (_options *ValidateReportsSnapshotConfigOptions) SetCosLocation(cosLocation string) *ValidateReportsSnapshotConfigOptions {
3896+
_options.CosLocation = core.StringPtr(cosLocation)
3897+
return _options
3898+
}
3899+
3900+
// SetCosReportsFolder : Allow user to set CosReportsFolder
3901+
func (_options *ValidateReportsSnapshotConfigOptions) SetCosReportsFolder(cosReportsFolder string) *ValidateReportsSnapshotConfigOptions {
3902+
_options.CosReportsFolder = core.StringPtr(cosReportsFolder)
3903+
return _options
3904+
}
3905+
3906+
// SetReportTypes : Allow user to set ReportTypes
3907+
func (_options *ValidateReportsSnapshotConfigOptions) SetReportTypes(reportTypes []string) *ValidateReportsSnapshotConfigOptions {
3908+
_options.ReportTypes = reportTypes
3909+
return _options
3910+
}
3911+
3912+
// SetVersioning : Allow user to set Versioning
3913+
func (_options *ValidateReportsSnapshotConfigOptions) SetVersioning(versioning string) *ValidateReportsSnapshotConfigOptions {
3914+
_options.Versioning = core.StringPtr(versioning)
3915+
return _options
3916+
}
3917+
3918+
// SetHeaders : Allow user to set Headers
3919+
func (options *ValidateReportsSnapshotConfigOptions) SetHeaders(param map[string]string) *ValidateReportsSnapshotConfigOptions {
3920+
options.Headers = param
3921+
return options
3922+
}
3923+
37033924
// GetResourceUsageAccountPager can be used to simplify the use of the "GetResourceUsageAccount" method.
37043925
type GetResourceUsageAccountPager struct {
37053926
hasNext bool

‎usagereportsv4/usage_reports_v4_examples_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,27 @@ var _ = Describe(`UsageReportsV4 Examples Tests`, func() {
369369
Expect(response.StatusCode).To(Equal(200))
370370
Expect(snapshotConfig).ToNot(BeNil())
371371
})
372+
It(`ValidateReportsSnapshotConfig request example`, func() {
373+
fmt.Println("\nValidateReportsSnapshotConfig() result:")
374+
// begin-validate_reports_snapshot_config
375+
376+
validateReportsSnapshotConfigOptions := usageReportsService.NewValidateReportsSnapshotConfigOptions(
377+
accountID,
378+
)
379+
380+
snapshotConfigValidateResponse, response, err := usageReportsService.ValidateReportsSnapshotConfig(validateReportsSnapshotConfigOptions)
381+
if err != nil {
382+
panic(err)
383+
}
384+
b, _ := json.MarshalIndent(snapshotConfigValidateResponse, "", " ")
385+
fmt.Println(string(b))
386+
387+
// end-validate_reports_snapshot_config
388+
389+
Expect(err).To(BeNil())
390+
Expect(response.StatusCode).To(Equal(200))
391+
Expect(snapshotConfigValidateResponse).ToNot(BeNil())
392+
})
372393
It(`GetReportsSnapshot request example`, func() {
373394
fmt.Println("\nGetReportsSnapshot() result:")
374395
// begin-get_reports_snapshot

‎usagereportsv4/usage_reports_v4_integration_test.go

+101
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,111 @@ var _ = Describe(`UsageReportsV4 Integration Tests`, func() {
433433
})
434434
})
435435

436+
Describe(`ValidateReportsSnapshotConfig - Verify billing to COS authorization`, func() {
437+
BeforeEach(func() {
438+
shouldSkipTest()
439+
})
440+
It(`ValidateReportsSnapshotConfig(validateReportsSnapshotConfigOptions *ValidateReportsSnapshotConfigOptions)`, func() {
441+
validateReportsSnapshotConfigOptions := &usagereportsv4.ValidateReportsSnapshotConfigOptions{
442+
AccountID: &accountID,
443+
Interval: core.StringPtr("daily"),
444+
CosBucket: &cosBucket,
445+
CosLocation: &cosLocation,
446+
CosReportsFolder: core.StringPtr("IBMCloud-Billing-Reports"),
447+
ReportTypes: []string{"account_summary", "enterprise_summary", "account_resource_instance_usage"},
448+
Versioning: core.StringPtr("new"),
449+
}
450+
451+
snapshotConfigValidateResponse, response, err := usageReportsService.ValidateReportsSnapshotConfig(validateReportsSnapshotConfigOptions)
452+
Expect(err).To(BeNil())
453+
Expect(response.StatusCode).To(Equal(200))
454+
Expect(snapshotConfigValidateResponse).ToNot(BeNil())
455+
})
456+
})
457+
436458
Describe(`GetReportsSnapshot - Fetch the current or past snapshots`, func() {
437459
BeforeEach(func() {
438460
shouldSkipTest()
439461
})
462+
It(`GetReportsSnapshot(getReportsSnapshotOptions *GetReportsSnapshotOptions) with pagination`, func() {
463+
getReportsSnapshotOptions := &usagereportsv4.GetReportsSnapshotOptions{
464+
AccountID: &accountID,
465+
Month: &billingMonth,
466+
Limit: core.Int64Ptr(int64(30)),
467+
Start: core.StringPtr("testString"),
468+
}
469+
from, err := strconv.ParseInt(dateFrom, 10, 64)
470+
if err != nil {
471+
panic(err)
472+
}
473+
to, err := strconv.ParseInt(dateTo, 10, 64)
474+
if err != nil {
475+
panic(err)
476+
}
477+
getReportsSnapshotOptions.SetDateFrom(from)
478+
getReportsSnapshotOptions.SetDateTo(to)
479+
480+
getReportsSnapshotOptions.Start = nil
481+
482+
var allResults []usagereportsv4.SnapshotListSnapshotsItem
483+
for {
484+
snapshotList, response, err := usageReportsService.GetReportsSnapshot(getReportsSnapshotOptions)
485+
Expect(err).To(BeNil())
486+
Expect(response.StatusCode).To(Equal(200))
487+
Expect(snapshotList).ToNot(BeNil())
488+
allResults = append(allResults, snapshotList.Snapshots...)
489+
490+
getReportsSnapshotOptions.Start, err = snapshotList.GetNextStart()
491+
Expect(err).To(BeNil())
492+
493+
if getReportsSnapshotOptions.Start == nil {
494+
break
495+
}
496+
}
497+
fmt.Fprintf(GinkgoWriter, "Retrieved a total of %d item(s) with pagination.\n", len(allResults))
498+
})
499+
It(`GetReportsSnapshot(getReportsSnapshotOptions *GetReportsSnapshotOptions) using GetReportsSnapshotPager`, func() {
500+
getReportsSnapshotOptions := &usagereportsv4.GetReportsSnapshotOptions{
501+
AccountID: &accountID,
502+
Month: &billingMonth,
503+
Limit: core.Int64Ptr(int64(30)),
504+
}
505+
from, err := strconv.ParseInt(dateFrom, 10, 64)
506+
if err != nil {
507+
panic(err)
508+
}
509+
to, err := strconv.ParseInt(dateTo, 10, 64)
510+
if err != nil {
511+
panic(err)
512+
}
513+
getReportsSnapshotOptions.SetDateFrom(from)
514+
getReportsSnapshotOptions.SetDateTo(to)
515+
516+
// Test GetNext().
517+
pager, err := usageReportsService.NewGetReportsSnapshotPager(getReportsSnapshotOptions)
518+
Expect(err).To(BeNil())
519+
Expect(pager).ToNot(BeNil())
520+
521+
var allResults []usagereportsv4.SnapshotListSnapshotsItem
522+
for pager.HasNext() {
523+
nextPage, err := pager.GetNext()
524+
Expect(err).To(BeNil())
525+
Expect(nextPage).ToNot(BeNil())
526+
allResults = append(allResults, nextPage...)
527+
}
528+
529+
// Test GetAll().
530+
pager, err = usageReportsService.NewGetReportsSnapshotPager(getReportsSnapshotOptions)
531+
Expect(err).To(BeNil())
532+
Expect(pager).ToNot(BeNil())
533+
534+
allItems, err := pager.GetAll()
535+
Expect(err).To(BeNil())
536+
Expect(allItems).ToNot(BeNil())
537+
538+
Expect(len(allItems)).To(Equal(len(allResults)))
539+
fmt.Fprintf(GinkgoWriter, "GetReportsSnapshot() returned a total of %d item(s) using GetReportsSnapshotPager.\n", len(allResults))
540+
})
440541
It(`GetReportsSnapshot(getReportsSnapshotOptions *GetReportsSnapshotOptions)`, func() {
441542
getReportsSnapshotOptions := &usagereportsv4.GetReportsSnapshotOptions{
442543
AccountID: &accountID,

0 commit comments

Comments
 (0)