Skip to content

Commit

Permalink
feat(Context-based Restrictions): add rule API type support
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen SooHoo <soohoos@us.ibm.com>
  • Loading branch information
soohoowoohoo authored and padamstx committed Jul 28, 2022
1 parent 3d79112 commit 27770ae
Show file tree
Hide file tree
Showing 3 changed files with 686 additions and 8 deletions.
275 changes: 275 additions & 0 deletions contextbasedrestrictionsv1/context_based_restrictions_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,9 @@ func (contextBasedRestrictions *ContextBasedRestrictionsV1) CreateRuleWithContex
if createRuleOptions.Resources != nil {
body["resources"] = createRuleOptions.Resources
}
if createRuleOptions.Operations != nil {
body["operations"] = createRuleOptions.Operations
}
if createRuleOptions.EnforcementMode != nil {
body["enforcement_mode"] = createRuleOptions.EnforcementMode
}
Expand Down Expand Up @@ -881,6 +884,9 @@ func (contextBasedRestrictions *ContextBasedRestrictionsV1) ReplaceRuleWithConte
if replaceRuleOptions.Resources != nil {
body["resources"] = replaceRuleOptions.Resources
}
if replaceRuleOptions.Operations != nil {
body["operations"] = replaceRuleOptions.Operations
}
if replaceRuleOptions.EnforcementMode != nil {
body["enforcement_mode"] = replaceRuleOptions.EnforcementMode
}
Expand Down Expand Up @@ -1030,6 +1036,108 @@ func (contextBasedRestrictions *ContextBasedRestrictionsV1) GetAccountSettingsWi
return
}

// ListAvailableServiceOperations : List available service operations
// This operation lists all available service operations.
func (contextBasedRestrictions *ContextBasedRestrictionsV1) ListAvailableServiceOperations(listAvailableServiceOperationsOptions *ListAvailableServiceOperationsOptions) (result *OperationsList, response *core.DetailedResponse, err error) {
return contextBasedRestrictions.ListAvailableServiceOperationsWithContext(context.Background(), listAvailableServiceOperationsOptions)
}

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

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

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

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

builder.AddQuery("service_name", fmt.Sprint(*listAvailableServiceOperationsOptions.ServiceName))

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, UnmarshalOperationsList)
if err != nil {
return
}
response.Result = result
}

return
}

// APIType : Service API Type details.
type APIType struct {
// The id of the API type.
APITypeID *string `json:"api_type_id" validate:"required"`

// The displayed name of the API type.
DisplayName *string `json:"display_name" validate:"required"`

// The description of the API type.
Description *string `json:"description" validate:"required"`

// The actions available for the API type.
Actions []Action `json:"actions" validate:"required"`
}

// UnmarshalAPIType unmarshals an instance of APIType from the specified map of raw messages.
func UnmarshalAPIType(m map[string]json.RawMessage, result interface{}) (err error) {
obj := new(APIType)
err = core.UnmarshalPrimitive(m, "api_type_id", &obj.APITypeID)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "display_name", &obj.DisplayName)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "description", &obj.Description)
if err != nil {
return
}
err = core.UnmarshalModel(m, "actions", &obj.Actions, UnmarshalAction)
if err != nil {
return
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj))
return
}

// AccountSettings : An output account settings.
type AccountSettings struct {
// The globally unique ID of the account settings.
Expand Down Expand Up @@ -1117,6 +1225,30 @@ func UnmarshalAccountSettings(m map[string]json.RawMessage, result interface{})
return
}

// Action : Service API Type actions.
type Action struct {
// The id of the action.
ActionID *string `json:"action_id" validate:"required"`

// The description of the action.
Description *string `json:"description" validate:"required"`
}

// UnmarshalAction unmarshals an instance of Action from the specified map of raw messages.
func UnmarshalAction(m map[string]json.RawMessage, result interface{}) (err error) {
obj := new(Action)
err = core.UnmarshalPrimitive(m, "action_id", &obj.ActionID)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "description", &obj.Description)
if err != nil {
return
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj))
return
}

// Address : A zone address.
// Models which "extend" this model:
// - AddressIPAddress
Expand Down Expand Up @@ -1193,6 +1325,9 @@ type CreateRuleOptions struct {
// The resources this rule apply to.
Resources []Resource `json:"resources,omitempty"`

// The operations this rule applies to.
Operations *NewRuleOperations `json:"operations,omitempty"`

// 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.
Expand Down Expand Up @@ -1247,6 +1382,12 @@ func (_options *CreateRuleOptions) SetResources(resources []Resource) *CreateRul
return _options
}

// SetOperations : Allow user to set Operations
func (_options *CreateRuleOptions) SetOperations(operations *NewRuleOperations) *CreateRuleOptions {
_options.Operations = operations
return _options
}

// SetEnforcementMode : Allow user to set EnforcementMode
func (_options *CreateRuleOptions) SetEnforcementMode(enforcementMode string) *CreateRuleOptions {
_options.EnforcementMode = core.StringPtr(enforcementMode)
Expand Down Expand Up @@ -1606,6 +1747,56 @@ func (options *GetZoneOptions) SetHeaders(param map[string]string) *GetZoneOptio
return options
}

// ListAvailableServiceOperationsOptions : The ListAvailableServiceOperations options.
type ListAvailableServiceOperationsOptions struct {
// The name of the service.
ServiceName *string `json:"service_name" validate:"required"`

// 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`.
TransactionID *string `json:"Transaction-Id,omitempty"`

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

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

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

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

// SetTransactionID : Allow user to set TransactionID
func (_options *ListAvailableServiceOperationsOptions) SetTransactionID(transactionID string) *ListAvailableServiceOperationsOptions {
_options.TransactionID = core.StringPtr(transactionID)
return _options
}

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

// ListAvailableServicerefTargetsOptions : The ListAvailableServicerefTargets options.
type ListAvailableServicerefTargetsOptions struct {
// The supplied or generated value of this header is logged for a request and repeated in a response header for the
Expand Down Expand Up @@ -1870,6 +2061,74 @@ func (options *ListZonesOptions) SetHeaders(param map[string]string) *ListZonesO
return options
}

// NewRuleOperations : The operations this rule applies to.
type NewRuleOperations struct {
// The API types this rule applies to.
APITypes []NewRuleOperationsAPITypesItem `json:"api_types" validate:"required"`
}

// NewNewRuleOperations : Instantiate NewRuleOperations (Generic Model Constructor)
func (*ContextBasedRestrictionsV1) NewNewRuleOperations(apiTypes []NewRuleOperationsAPITypesItem) (_model *NewRuleOperations, err error) {
_model = &NewRuleOperations{
APITypes: apiTypes,
}
err = core.ValidateStruct(_model, "required parameters")
return
}

// UnmarshalNewRuleOperations unmarshals an instance of NewRuleOperations from the specified map of raw messages.
func UnmarshalNewRuleOperations(m map[string]json.RawMessage, result interface{}) (err error) {
obj := new(NewRuleOperations)
err = core.UnmarshalModel(m, "api_types", &obj.APITypes, UnmarshalNewRuleOperationsAPITypesItem)
if err != nil {
return
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj))
return
}

// NewRuleOperationsAPITypesItem : NewRuleOperationsAPITypesItem struct
type NewRuleOperationsAPITypesItem struct {
APITypeID *string `json:"api_type_id" validate:"required"`
}

// NewNewRuleOperationsAPITypesItem : Instantiate NewRuleOperationsAPITypesItem (Generic Model Constructor)
func (*ContextBasedRestrictionsV1) NewNewRuleOperationsAPITypesItem(apiTypeID string) (_model *NewRuleOperationsAPITypesItem, err error) {
_model = &NewRuleOperationsAPITypesItem{
APITypeID: core.StringPtr(apiTypeID),
}
err = core.ValidateStruct(_model, "required parameters")
return
}

// UnmarshalNewRuleOperationsAPITypesItem unmarshals an instance of NewRuleOperationsAPITypesItem from the specified map of raw messages.
func UnmarshalNewRuleOperationsAPITypesItem(m map[string]json.RawMessage, result interface{}) (err error) {
obj := new(NewRuleOperationsAPITypesItem)
err = core.UnmarshalPrimitive(m, "api_type_id", &obj.APITypeID)
if err != nil {
return
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj))
return
}

// OperationsList : The response object of the `list_available_service_operations` operation.
type OperationsList struct {
// The returned API types.
APITypes []APIType `json:"api_types" validate:"required"`
}

// UnmarshalOperationsList unmarshals an instance of OperationsList from the specified map of raw messages.
func UnmarshalOperationsList(m map[string]json.RawMessage, result interface{}) (err error) {
obj := new(OperationsList)
err = core.UnmarshalModel(m, "api_types", &obj.APITypes, UnmarshalAPIType)
if err != nil {
return
}
reflect.ValueOf(result).Elem().Set(reflect.ValueOf(obj))
return
}

// ReplaceRuleOptions : The ReplaceRule options.
type ReplaceRuleOptions struct {
// The ID of a rule.
Expand All @@ -1888,6 +2147,9 @@ type ReplaceRuleOptions struct {
// The resources this rule apply to.
Resources []Resource `json:"resources,omitempty"`

// The operations this rule applies to.
Operations *NewRuleOperations `json:"operations,omitempty"`

// 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.
Expand Down Expand Up @@ -1957,6 +2219,12 @@ func (_options *ReplaceRuleOptions) SetResources(resources []Resource) *ReplaceR
return _options
}

// SetOperations : Allow user to set Operations
func (_options *ReplaceRuleOptions) SetOperations(operations *NewRuleOperations) *ReplaceRuleOptions {
_options.Operations = operations
return _options
}

// SetEnforcementMode : Allow user to set EnforcementMode
func (_options *ReplaceRuleOptions) SetEnforcementMode(enforcementMode string) *ReplaceRuleOptions {
_options.EnforcementMode = core.StringPtr(enforcementMode)
Expand Down Expand Up @@ -2220,6 +2488,9 @@ type Rule struct {
// The resources this rule apply to.
Resources []Resource `json:"resources" validate:"required"`

// The operations this rule applies to.
Operations *NewRuleOperations `json:"operations,omitempty"`

// 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.
Expand Down Expand Up @@ -2276,6 +2547,10 @@ func UnmarshalRule(m map[string]json.RawMessage, result interface{}) (err error)
if err != nil {
return
}
err = core.UnmarshalModel(m, "operations", &obj.Operations, UnmarshalNewRuleOperations)
if err != nil {
return
}
err = core.UnmarshalPrimitive(m, "enforcement_mode", &obj.EnforcementMode)
if err != nil {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,28 @@ var _ = Describe(`ContextBasedRestrictionsV1 Examples Tests`, func() {

})

It(`ListAvailableServiceOperations request example`, func() {
fmt.Println("\nListAvailableServiceOperations() result:")
// begin-list_available_service_operations

listAvailableServiceOperationsOptions := contextBasedRestrictionsService.NewListAvailableServiceOperationsOptions(
"containers-kubernetes",
)

operationsList, response, err := contextBasedRestrictionsService.ListAvailableServiceOperations(listAvailableServiceOperationsOptions)
if err != nil {
panic(err)
}
b, _ := json.MarshalIndent(operationsList, "", " ")
fmt.Println(string(b))

// end-list_available_service_operations

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

It(`DeleteRule request example`, func() {
// begin-delete_rule

Expand Down

0 comments on commit 27770ae

Please sign in to comment.