Move actions, resource (types), and scopes to packages. (#149)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-06-26 11:00:14 -04:00
1 parent 9d1d0ba08e
commit d8f11af4d7
37 files changed
+721 -460

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
package action
// Type defines a type for the Actions of Resources
// actions are also stored as a lookup db table named iam_action
type Type int
// not using iota intentionally, since the values are stored in the db as well.
const (
Unknown Type = 0
List Type = 1
Create Type = 2
Update Type = 3
Read Type = 4
Delete Type = 5
Authen Type = 6
All Type = 7
Connect Type = 8
)
var Map = map[string]Type{
"unknown": Unknown,
"list": List,
"create": Create,
"update": Update,
"read": Read,
"delete": Delete,
"authen": Authen,
"*": All,
"connect": Connect,
}
func (a Type) String() string {
return [...]string{
"unknown",
"list",
"create",
"update",
"read",
"delete",
"authen",
"*",
"connect",
}[a]
}
+57
View File
@@ -0,0 +1,57 @@
package action
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAction(t *testing.T) {
tests := []struct {
action Type
want string
}{
{
action: Unknown,
want: "unknown",
},
{
action: List,
want: "list",
},
{
action: Create,
want: "create",
},
{
action: Update,
want: "update",
},
{
action: Read,
want: "read",
},
{
action: Delete,
want: "delete",
},
{
action: Authen,
want: "authen",
},
{
action: All,
want: "*",
},
{
action: Connect,
want: "connect",
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.action.String(), "unexpected action string for %s", tt.want)
assert.Equalf(t, tt.action, Map[tt.want], "unexpected action from map for %s", tt.want)
})
}
}
+93
View File
@@ -0,0 +1,93 @@
package resource
// Type defines the types of resources in the system
type Type int
const (
Unknown Type = 0
Scope Type = 1
User Type = 2
StaticGroup Type = 3
Role Type = 4
Organization Type = 5
StaticGroupMember Type = 6
StaticGroupUserMember Type = 7
AssignedRole Type = 8
AssignedUserRole Type = 9
AssignedStaticGroupRole Type = 10
RoleGrant Type = 11
AuthMethod Type = 12
Project Type = 13
All Type = 14
HostCatalog Type = 15
HostSet Type = 16
Host Type = 17
Target Type = 18
)
func (r Type) String() string {
return [...]string{
"unknown",
"scope",
"user",
"static-group",
"role",
"organization",
"static-group-member",
"static-group-user-member",
"assigned-role",
"assigned-user-role",
"assigned-static-group-role",
"role-grant",
"auth-method",
"project",
"*",
"host-catalog",
"host-set",
"host",
"target",
}[r]
}
func StringToResourceType(s string) Type {
switch s {
case Scope.String():
return Scope
case User.String():
return User
case StaticGroup.String():
return StaticGroup
case Role.String():
return Role
case Organization.String():
return Organization
case StaticGroupMember.String():
return StaticGroupMember
case StaticGroupUserMember.String():
return StaticGroupUserMember
case AssignedRole.String():
return AssignedRole
case AssignedUserRole.String():
return AssignedUserRole
case AssignedStaticGroupRole.String():
return AssignedStaticGroupRole
case RoleGrant.String():
return RoleGrant
case AuthMethod.String():
return AuthMethod
case Project.String():
return Project
case All.String():
return All
case HostCatalog.String():
return HostCatalog
case HostSet.String():
return HostSet
case Host.String():
return Host
case Target.String():
return Target
default:
return Unknown
}
}
+98
View File
@@ -0,0 +1,98 @@
package resource
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Resource(t *testing.T) {
tests := []struct {
typeString string
want Type
}{
{
typeString: "unknown",
want: Unknown,
},
{
typeString: "scope",
want: Scope,
},
{
typeString: "user",
want: User,
},
{
typeString: "static-group",
want: StaticGroup,
},
{
typeString: "role",
want: Role,
},
{
typeString: "organization",
want: Organization,
},
{
typeString: "static-group-member",
want: StaticGroupMember,
},
{
typeString: "static-group-user-member",
want: StaticGroupUserMember,
},
{
typeString: "assigned-role",
want: AssignedRole,
},
{
typeString: "assigned-user-role",
want: AssignedUserRole,
},
{
typeString: "assigned-static-group-role",
want: AssignedStaticGroupRole,
},
{
typeString: "role-grant",
want: RoleGrant,
},
{
typeString: "auth-method",
want: AuthMethod,
},
{
typeString: "project",
want: Project,
},
{
typeString: "*",
want: All,
},
{
typeString: "host-catalog",
want: HostCatalog,
},
{
typeString: "host-set",
want: HostSet,
},
{
typeString: "host",
want: Host,
},
{
typeString: "target",
want: Target,
},
}
for _, tt := range tests {
t.Run(tt.typeString, func(t *testing.T) {
assert.Equalf(t, tt.want, StringToResourceType(tt.typeString), "unexpected type for %s", tt.typeString)
assert.Equalf(t, tt.typeString, tt.want.String(), "unexpected string for %s", tt.typeString)
})
}
}
+37
View File
@@ -0,0 +1,37 @@
package scope
// Type defines the possible types for Scopes
type Type uint32
const (
Unknown Type = 0
Organization Type = 1
Project Type = 2
)
func (s Type) String() string {
return [...]string{
"unknown",
"organization",
"project",
}[s]
}
func (s Type) Prefix() string {
return [...]string{
"unknown",
"o",
"p",
}[s]
}
func StringToScopeType(s string) Type {
switch s {
case Organization.String():
return Organization
case Project.String():
return Project
default:
return Unknown
}
}
+43
View File
@@ -0,0 +1,43 @@
package scope
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_StringToScopeType(t *testing.T) {
tests := []struct {
name string
s string
want Type
wantPrefix string
}{
{
name: "org",
s: "organization",
want: Organization,
wantPrefix: "o",
},
{
name: "proj",
s: "project",
want: Project,
wantPrefix: "p",
},
{
name: "unknown",
s: "org",
want: Unknown,
wantPrefix: "unknown",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)
got := StringToScopeType(tt.s)
assert.Equal(tt.want, got)
assert.Equalf(tt.wantPrefix, got.Prefix(), "unexpected prefix for %s", tt.s)
})
}
}