-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create application profile definition builder
- Loading branch information
1 parent
120c07f
commit 5432001
Showing
7 changed files
with
287 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cache | ||
|
||
import v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" | ||
|
||
// Access Request Definition cache management | ||
|
||
// AddApplicationProfileDefinition - add/update ApplicationProfileDefinition resource in cache | ||
func (c *cacheManager) AddApplicationProfileDefinition(resource *v1.ResourceInstance) { | ||
defer c.setCacheUpdated(true) | ||
|
||
c.ardMap.SetWithSecondaryKey(resource.Metadata.ID, resource.Name, resource) | ||
} | ||
|
||
// GetApplicationProfileDefinitionKeys - returns keys for ApplicationProfileDefinition cache | ||
func (c *cacheManager) GetApplicationProfileDefinitionKeys() []string { | ||
c.ApplyResourceReadLock() | ||
defer c.ReleaseResourceReadLock() | ||
|
||
return c.ardMap.GetKeys() | ||
} | ||
|
||
// GetApplicationProfileDefinitionByName - returns resource from ApplicationProfileDefinition cache based on resource name | ||
func (c *cacheManager) GetApplicationProfileDefinitionByName(name string) (*v1.ResourceInstance, error) { | ||
c.ApplyResourceReadLock() | ||
defer c.ReleaseResourceReadLock() | ||
|
||
item, err := c.ardMap.GetBySecondaryKey(name) | ||
if item != nil { | ||
if ard, ok := item.(*v1.ResourceInstance); ok { | ||
ard.CreateHashes() | ||
return ard, nil | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
// GetApplicationProfileDefinitionByID - returns resource from ApplicationProfileDefinition cache based on resource id | ||
func (c *cacheManager) GetApplicationProfileDefinitionByID(id string) (*v1.ResourceInstance, error) { | ||
c.ApplyResourceReadLock() | ||
defer c.ReleaseResourceReadLock() | ||
|
||
item, err := c.ardMap.Get(id) | ||
if item != nil { | ||
if ard, ok := item.(*v1.ResourceInstance); ok { | ||
ard.CreateHashes() | ||
return ard, nil | ||
} | ||
} | ||
return nil, err | ||
} | ||
|
||
// DeleteApplicationProfileDefinition - deletes the ApplicationProfileDefinition cache based on resource id | ||
func (c *cacheManager) DeleteApplicationProfileDefinition(id string) error { | ||
defer c.setCacheUpdated(true) | ||
|
||
return c.ardMap.Delete(id) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
pkg/apic/provisioning/applicationprofiledefinitionbuilder.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package provisioning | ||
|
||
import ( | ||
"fmt" | ||
|
||
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" | ||
"github.com/Axway/agent-sdk/pkg/apic/definitions" | ||
"github.com/Axway/agent-sdk/pkg/util" | ||
) | ||
|
||
// RegisterApplicationProfileDefinition - the function signature used when calling the NewApplicationProfileBuilder function | ||
type RegisterApplicationProfileDefinition func(applicationProfileDefinition *management.ApplicationProfileDefinition) (*management.ApplicationProfileDefinition, error) | ||
|
||
type applicationProfileDef struct { | ||
name string | ||
title string | ||
requestSchema map[string]interface{} | ||
registerFunc RegisterApplicationProfileDefinition | ||
err error | ||
} | ||
|
||
// ApplicationProfileBuilder - aids in creating a new access request | ||
type ApplicationProfileBuilder interface { | ||
SetName(name string) ApplicationProfileBuilder | ||
SetTitle(title string) ApplicationProfileBuilder | ||
SetRequestSchema(schema SchemaBuilder) ApplicationProfileBuilder | ||
Register() (*management.ApplicationProfileDefinition, error) | ||
} | ||
|
||
// NewApplicationProfileBuilder - called by the agent package and sends in the function that registers this access request | ||
func NewApplicationProfileBuilder(registerFunc RegisterApplicationProfileDefinition) ApplicationProfileBuilder { | ||
return &applicationProfileDef{ | ||
registerFunc: registerFunc, | ||
} | ||
} | ||
|
||
// SetName - set the name of the access request | ||
func (a *applicationProfileDef) SetName(name string) ApplicationProfileBuilder { | ||
a.name = name | ||
return a | ||
} | ||
|
||
// SetTitle - set the title of the access request | ||
func (a *applicationProfileDef) SetTitle(title string) ApplicationProfileBuilder { | ||
a.title = title | ||
return a | ||
} | ||
|
||
// SetRequestSchema - set the schema to be used for access requests request data | ||
func (a *applicationProfileDef) SetRequestSchema(schema SchemaBuilder) ApplicationProfileBuilder { | ||
if a.err != nil { | ||
return a | ||
} | ||
|
||
if schema != nil { | ||
a.requestSchema, a.err = schema.Build() | ||
} else { | ||
a.err = fmt.Errorf("expected a SchemaBuilder argument but received nil") | ||
} | ||
|
||
return a | ||
} | ||
|
||
// Register - create the access request defintion and send it to Central | ||
func (a *applicationProfileDef) Register() (*management.ApplicationProfileDefinition, error) { | ||
if a.err != nil { | ||
return nil, a.err | ||
} | ||
|
||
if a.requestSchema == nil { | ||
a.requestSchema, _ = NewSchemaBuilder().Build() | ||
} | ||
|
||
if a.title == "" { | ||
a.title = a.name | ||
} | ||
|
||
spec := management.ApplicationProfileDefinitionSpec{ | ||
Schema: a.requestSchema, | ||
} | ||
|
||
hashInt, _ := util.ComputeHash(spec) | ||
|
||
if a.name == "" { | ||
a.name = util.ConvertUnitToString(hashInt) | ||
} | ||
|
||
ard := management.NewApplicationProfileDefinition(a.name, "") | ||
ard.Title = a.title | ||
ard.Spec = spec | ||
|
||
util.SetAgentDetailsKey(ard, definitions.AttrSpecHash, fmt.Sprintf("%v", hashInt)) | ||
|
||
return a.registerFunc(ard) | ||
} |
77 changes: 77 additions & 0 deletions
77
pkg/apic/provisioning/applicationprofiledefinitionbuilder_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package provisioning | ||
|
||
import ( | ||
"testing" | ||
|
||
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewApplicationProfileBuilder(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
noSchema bool | ||
copySchema bool | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "Success", | ||
}, | ||
{ | ||
name: "Fail", | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "Empty", | ||
noSchema: true, | ||
}, | ||
{ | ||
name: "Copied", | ||
copySchema: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
registerFuncCalled := false | ||
registerFunc := func(applicationProfileDefinition *management.ApplicationProfileDefinition) (*management.ApplicationProfileDefinition, error) { | ||
assert.NotNil(t, applicationProfileDefinition) | ||
if !tt.noSchema { | ||
assert.Len(t, applicationProfileDefinition.Spec.Schema["properties"], 1) | ||
assert.NotNil(t, applicationProfileDefinition.Spec.Schema["properties"].(map[string]interface{})["prop"]) | ||
} else { | ||
assert.Len(t, applicationProfileDefinition.Spec.Schema["properties"], 0) | ||
} | ||
registerFuncCalled = true | ||
return nil, nil | ||
} | ||
|
||
builder := NewApplicationProfileBuilder(registerFunc). | ||
SetName(tt.name) | ||
|
||
if tt.wantErr { | ||
builder = builder.SetRequestSchema(nil) | ||
} | ||
|
||
if !tt.noSchema { | ||
builder = builder. | ||
SetRequestSchema( | ||
NewSchemaBuilder(). | ||
SetName("schema"). | ||
AddProperty( | ||
NewSchemaPropertyBuilder(). | ||
SetName("prop"). | ||
IsString())) | ||
} | ||
|
||
_, err := builder.Register() | ||
|
||
if tt.wantErr { | ||
assert.NotNil(t, err) | ||
assert.False(t, registerFuncCalled) | ||
} else { | ||
assert.Nil(t, err) | ||
assert.True(t, registerFuncCalled) | ||
} | ||
}) | ||
} | ||
} |