Skip to content

Commit

Permalink
feat(Context-Based Restrictions): add Service Ref Target (#306)
Browse files Browse the repository at this point in the history
Signed-off-by: Dylan <Dylan.Yu@ibm.com>
  • Loading branch information
JonahFarc committed Mar 8, 2024
1 parent 02efac8 commit 6cefbb1
Show file tree
Hide file tree
Showing 5 changed files with 742 additions and 21 deletions.
168 changes: 157 additions & 11 deletions contextbasedrestrictionsv1/context_based_restrictions_v1.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2023.
* (C) Copyright IBM Corp. 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
*/

/*
* IBM OpenAPI SDK Code Generator Version: 3.81.0-c73a091c-20231026-215706
* IBM OpenAPI SDK Code Generator Version: 3.86.0-bc6f14b3-20240221-193958
*/

// Package contextbasedrestrictionsv1 : Operations and models for the ContextBasedRestrictionsV1 service
Expand Down Expand Up @@ -587,6 +587,72 @@ func (contextBasedRestrictions *ContextBasedRestrictionsV1) ListAvailableService
return
}

// GetServicerefTarget : Get service reference target for a specified service name
// This operation gets the service reference target for a specified service name.
func (contextBasedRestrictions *ContextBasedRestrictionsV1) GetServicerefTarget(getServicerefTargetOptions *GetServicerefTargetOptions) (result *ServiceRefTarget, response *core.DetailedResponse, err error) {
return contextBasedRestrictions.GetServicerefTargetWithContext(context.Background(), getServicerefTargetOptions)
}

// GetServicerefTargetWithContext is an alternate form of the GetServicerefTarget method which supports a Context parameter
func (contextBasedRestrictions *ContextBasedRestrictionsV1) GetServicerefTargetWithContext(ctx context.Context, getServicerefTargetOptions *GetServicerefTargetOptions) (result *ServiceRefTarget, response *core.DetailedResponse, err error) {
err = core.ValidateNotNil(getServicerefTargetOptions, "getServicerefTargetOptions cannot be nil")
if err != nil {
return
}
err = core.ValidateStruct(getServicerefTargetOptions, "getServicerefTargetOptions")
if err != nil {
return
}

pathParamsMap := map[string]string{
"service_name": *getServicerefTargetOptions.ServiceName,
}

builder := core.NewRequestBuilder(core.GET)
builder = builder.WithContext(ctx)
builder.EnableGzipCompression = contextBasedRestrictions.GetEnableGzipCompression()
_, err = builder.ResolveRequestURL(contextBasedRestrictions.Service.Options.URL, `/v1/zones/serviceref_targets/{service_name}`, pathParamsMap)
if err != nil {
return
}

for headerName, headerValue := range getServicerefTargetOptions.Headers {
builder.AddHeader(headerName, headerValue)
}

sdkHeaders := common.GetSdkHeaders("context_based_restrictions", "V1", "GetServicerefTarget")
for headerName, headerValue := range sdkHeaders {
builder.AddHeader(headerName, headerValue)
}
builder.AddHeader("Accept", "application/json")
if getServicerefTargetOptions.XCorrelationID != nil {
builder.AddHeader("X-Correlation-Id", fmt.Sprint(*getServicerefTargetOptions.XCorrelationID))
}
if getServicerefTargetOptions.TransactionID != nil {
builder.AddHeader("Transaction-Id", fmt.Sprint(*getServicerefTargetOptions.TransactionID))
}

request, err := builder.Build()
if err != nil {
return
}

var rawResponse map[string]json.RawMessage
response, err = contextBasedRestrictions.Service.Request(request, &rawResponse)
if err != nil {
return
}
if rawResponse != nil {
err = core.UnmarshalModel(rawResponse, "", &result, UnmarshalServiceRefTarget)
if err != nil {
return
}
response.Result = result
}

return
}

// CreateRule : Create a rule
// This operation creates a rule for the specified account.
func (contextBasedRestrictions *ContextBasedRestrictionsV1) CreateRule(createRuleOptions *CreateRuleOptions) (result *Rule, response *core.DetailedResponse, err error) {
Expand Down Expand Up @@ -1173,12 +1239,18 @@ type AccountSettings struct {
// the max number of zones allowed for the account.
ZoneCountLimit *int64 `json:"zone_count_limit" validate:"required"`

// the max number of rules with tags allowed for the account.
TagsRuleCountLimit *int64 `json:"tags_rule_count_limit,omitempty"`

// the current number of rules used by the account.
CurrentRuleCount *int64 `json:"current_rule_count" validate:"required"`

// the current number of zones used by the account.
CurrentZoneCount *int64 `json:"current_zone_count" validate:"required"`

// the current number of rules with tags used by the account.
CurrentTagsRuleCount *int64 `json:"current_tags_rule_count,omitempty"`

// The href link to the resource.
Href *string `json:"href" validate:"required"`

Expand Down Expand Up @@ -1214,6 +1286,10 @@ func UnmarshalAccountSettings(m map[string]json.RawMessage, result interface{})
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "tags_rule_count_limit", &obj.TagsRuleCountLimit)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "current_rule_count", &obj.CurrentRuleCount)
if err != nil {
return
Expand All @@ -1222,6 +1298,10 @@ func UnmarshalAccountSettings(m map[string]json.RawMessage, result interface{})
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "current_tags_rule_count", &obj.CurrentTagsRuleCount)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "href", &obj.Href)
if err != nil {
return
Expand Down Expand Up @@ -1372,9 +1452,9 @@ type CreateRuleOptions struct {

// Constants associated with the CreateRuleOptions.EnforcementMode property.
// The rule enforcement mode:
// * `enabled` - The restrictions are enforced and reported. This is the default.
// * `disabled` - The restrictions are disabled. Nothing is enforced or reported.
// * `report` - The restrictions are evaluated and reported, but not enforced.
// - `enabled` - The restrictions are enforced and reported. This is the default.
// - `disabled` - The restrictions are disabled. Nothing is enforced or reported.
// - `report` - The restrictions are evaluated and reported, but not enforced.
const (
CreateRuleOptionsEnforcementModeDisabledConst = "disabled"
CreateRuleOptionsEnforcementModeEnabledConst = "enabled"
Expand Down Expand Up @@ -1730,6 +1810,58 @@ func (options *GetRuleOptions) SetHeaders(param map[string]string) *GetRuleOptio
return options
}

// GetServicerefTargetOptions : The GetServicerefTarget options.
type GetServicerefTargetOptions struct {
// The name of a service.
ServiceName *string `json:"service_name" validate:"required,ne="`

// The supplied or generated value of this header is logged for a request and repeated in a response header for the
// corresponding response. The same value is used for downstream requests and retries of those requests. If a value of
// this headers is not supplied in a request, the service generates a random (version 4) UUID.
XCorrelationID *string `json:"X-Correlation-Id,omitempty"`

// The `Transaction-Id` header behaves as the `X-Correlation-Id` header. It is supported for backward compatibility
// with other IBM platform services that support the `Transaction-Id` header only. If both `X-Correlation-Id` and
// `Transaction-Id` are provided, `X-Correlation-Id` has the precedence over `Transaction-Id`.
// Deprecated: this field is deprecated and may be removed in a future release.
TransactionID *string `json:"Transaction-Id,omitempty"`

// Allows users to set headers on API requests
Headers map[string]string
}

// NewGetServicerefTargetOptions : Instantiate GetServicerefTargetOptions
func (*ContextBasedRestrictionsV1) NewGetServicerefTargetOptions(serviceName string) *GetServicerefTargetOptions {
return &GetServicerefTargetOptions{
ServiceName: core.StringPtr(serviceName),
}
}

// SetServiceName : Allow user to set ServiceName
func (_options *GetServicerefTargetOptions) SetServiceName(serviceName string) *GetServicerefTargetOptions {
_options.ServiceName = core.StringPtr(serviceName)
return _options
}

// SetXCorrelationID : Allow user to set XCorrelationID
func (_options *GetServicerefTargetOptions) SetXCorrelationID(xCorrelationID string) *GetServicerefTargetOptions {
_options.XCorrelationID = core.StringPtr(xCorrelationID)
return _options
}

// SetTransactionID : Allow user to set TransactionID
// Deprecated: this method is deprecated and may be removed in a future release.
func (_options *GetServicerefTargetOptions) SetTransactionID(transactionID string) *GetServicerefTargetOptions {
_options.TransactionID = core.StringPtr(transactionID)
return _options
}

// SetHeaders : Allow user to set Headers
func (options *GetServicerefTargetOptions) SetHeaders(param map[string]string) *GetServicerefTargetOptions {
options.Headers = param
return options
}

// GetZoneOptions : The GetZone options.
type GetZoneOptions struct {
// The ID of a zone.
Expand Down Expand Up @@ -2241,9 +2373,9 @@ type ReplaceRuleOptions struct {

// Constants associated with the ReplaceRuleOptions.EnforcementMode property.
// The rule enforcement mode:
// * `enabled` - The restrictions are enforced and reported. This is the default.
// * `disabled` - The restrictions are disabled. Nothing is enforced or reported.
// * `report` - The restrictions are evaluated and reported, but not enforced.
// - `enabled` - The restrictions are enforced and reported. This is the default.
// - `disabled` - The restrictions are disabled. Nothing is enforced or reported.
// - `report` - The restrictions are evaluated and reported, but not enforced.
const (
ReplaceRuleOptionsEnforcementModeDisabledConst = "disabled"
ReplaceRuleOptionsEnforcementModeEnabledConst = "enabled"
Expand Down Expand Up @@ -2587,9 +2719,9 @@ type Rule struct {

// Constants associated with the Rule.EnforcementMode property.
// The rule enforcement mode:
// * `enabled` - The restrictions are enforced and reported. This is the default.
// * `disabled` - The restrictions are disabled. Nothing is enforced or reported.
// * `report` - The restrictions are evaluated and reported, but not enforced.
// - `enabled` - The restrictions are enforced and reported. This is the default.
// - `disabled` - The restrictions are disabled. Nothing is enforced or reported.
// - `report` - The restrictions are evaluated and reported, but not enforced.
const (
RuleEnforcementModeDisabledConst = "disabled"
RuleEnforcementModeEnabledConst = "enabled"
Expand Down Expand Up @@ -2792,13 +2924,27 @@ func UnmarshalServiceRefTargetList(m map[string]json.RawMessage, result interfac

// ServiceRefTargetLocationsItem : ServiceRefTargetLocationsItem struct
type ServiceRefTargetLocationsItem struct {
// The location display name.
DisplayName *string `json:"display_name,omitempty"`

// The location kind.
Kind *string `json:"kind,omitempty"`

// The location name.
Name *string `json:"name" validate:"required"`
}

// UnmarshalServiceRefTargetLocationsItem unmarshals an instance of ServiceRefTargetLocationsItem from the specified map of raw messages.
func UnmarshalServiceRefTargetLocationsItem(m map[string]json.RawMessage, result interface{}) (err error) {
obj := new(ServiceRefTargetLocationsItem)
err = core.UnmarshalPrimitive(m, "display_name", &obj.DisplayName)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "kind", &obj.Kind)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "name", &obj.Name)
if err != nil {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
. "github.com/onsi/gomega"
)

//
// This file provides an example of how to use the Context Based Restrictions service.
//
// The following configuration properties are assumed to be defined:
Expand All @@ -44,7 +43,6 @@ import (
// These configuration properties can be exported as environment variables, or stored
// in a configuration file and then:
// export IBM_CREDENTIALS_FILE=<name of configuration file>
//
var _ = Describe(`ContextBasedRestrictionsV1 Examples Tests`, func() {

const externalConfigFile = "../context_based_restrictions_v1.env"
Expand Down Expand Up @@ -292,6 +290,30 @@ var _ = Describe(`ContextBasedRestrictionsV1 Examples Tests`, func() {
Expect(serviceRefTargetList).ToNot(BeNil())

})
It(`GetServicerefTarget request example`, func() {
fmt.Println("\nGetServicerefTarget() result:")
tempServiceName := serviceName
serviceName = "containers-kubernetes"
// begin-get_serviceref_target

getServicerefTargetOptions := contextBasedRestrictionsService.NewGetServicerefTargetOptions(
serviceName,
)

serviceRefTarget, response, err := contextBasedRestrictionsService.GetServicerefTarget(getServicerefTargetOptions)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(serviceRefTarget, "", " ")
fmt.Println(string(b))

// end-get_serviceref_target
serviceName = tempServiceName

Expect(err).To(BeNil())
Expect(response.StatusCode).To(Equal(200))
Expect(serviceRefTarget).ToNot(BeNil())
})
It(`CreateRule request example`, func() {
fmt.Println("\nCreateRule() result:")
// begin-create_rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,40 @@ var _ = Describe(`ContextBasedRestrictionsV1 Integration Tests`, func() {
})
})

Describe(`GetServicerefTarget - Get service reference target for a specified service name`, func() {
BeforeEach(func() {
shouldSkipTest()
})
It(`GetServicerefTarget(getServicerefTargetOptions *GetServicerefTargetOptions)`, func() {
getServicerefTargetOptions := &contextbasedrestrictionsv1.GetServicerefTargetOptions{
ServiceName: core.StringPtr("containers-kubernetes"),
}

serviceRefTarget, response, err := contextBasedRestrictionsService.GetServicerefTarget(getServicerefTargetOptions)

Expect(err).To(BeNil())
Expect(response.StatusCode).To(Equal(200))
Expect(serviceRefTarget).ToNot(BeNil())
})
})

Describe(`GetServicerefTarget - Get service reference target for a specified service name with 'service_not_found' error `, func() {
BeforeEach(func() {
shouldSkipTest()
})
It(`GetServicerefTarget(getServicerefTargetOptions *GetServicerefTargetOptions)`, func() {
getServicerefTargetOptions := &contextbasedrestrictionsv1.GetServicerefTargetOptions{
ServiceName: core.StringPtr("invalid-service"),
}

serviceRefTarget, response, err := contextBasedRestrictionsService.GetServicerefTarget(getServicerefTargetOptions)

Expect(err).To(Not(BeNil()))
Expect(response.StatusCode).To(Equal(404))
Expect(serviceRefTarget).To(BeNil())
})
})

Describe(`CreateRule - Create a rule`, func() {
BeforeEach(func() {
shouldSkipTest()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* (C) Copyright IBM Corp. 2023.
* (C) Copyright IBM Corp. 2024.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 6cefbb1

Please sign in to comment.