Skip to content

Commit

Permalink
Merge pull request #481 from mgleung/id_string
Browse files Browse the repository at this point in the history
Add ID fields to Resource Objects for easy lookup and validations
  • Loading branch information
robbrockbank authored Jul 26, 2017
2 parents 9171efc + 01323ec commit 73b7a64
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/api/bgppeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package api

import (
"fmt"

"github.com/projectcalico/libcalico-go/lib/api/unversioned"
"github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/numorstring"
Expand All @@ -37,6 +39,15 @@ func (t BGPPeer) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of a BGPPeer instance
// which is defined by its PeerIP and Scope.
func (t BGPPeer) String() string {
if t.Metadata.Scope == scope.Node && t.Metadata.Node == "" {
return fmt.Sprintf("BGPPeer(PeerIP=%s, Scope=%s)", t.Metadata.PeerIP.IP.String(), t.Metadata.Scope)
}
return fmt.Sprintf("BGPPeer(PeerIP=%s, Scope=%s, Node=%s)", t.Metadata.PeerIP.IP.String(), t.Metadata.Scope, t.Metadata.Node)
}

// BGPPeerMetadata contains the metadata for a BGPPeer resource.
type BGPPeerMetadata struct {
unversioned.ObjectMetadata
Expand Down
8 changes: 8 additions & 0 deletions lib/api/hostendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package api

import (
"fmt"

"github.com/projectcalico/libcalico-go/lib/api/unversioned"
"github.com/projectcalico/libcalico-go/lib/net"
)
Expand All @@ -31,6 +33,12 @@ func (t HostEndpoint) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of a HostEndpoint instance
// which is defined by its Node and Name.
func (t HostEndpoint) String() string {
return fmt.Sprintf("HostEndpoint(Node=%s, Name=%s)", t.Metadata.Node, t.Metadata.Name)
}

// HostEndpointMetadata contains the Metadata for a HostEndpoint resource.
type HostEndpointMetadata struct {
unversioned.ObjectMetadata
Expand Down
8 changes: 8 additions & 0 deletions lib/api/ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package api

import (
"fmt"

"github.com/projectcalico/libcalico-go/lib/api/unversioned"
"github.com/projectcalico/libcalico-go/lib/ipip"
"github.com/projectcalico/libcalico-go/lib/net"
Expand All @@ -37,6 +39,12 @@ func (t IPPool) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of an IPPool instance
// which is defined by its CIDR.
func (t IPPool) String() string {
return fmt.Sprintf("IPPool(CIDR=%s)", t.Metadata.CIDR.String())
}

// IPPoolMetadata contains the metadata for an IP pool resource.
type IPPoolMetadata struct {
unversioned.ObjectMetadata
Expand Down
8 changes: 8 additions & 0 deletions lib/api/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package api

import (
"fmt"

"github.com/projectcalico/libcalico-go/lib/api/unversioned"
"github.com/projectcalico/libcalico-go/lib/net"
"github.com/projectcalico/libcalico-go/lib/numorstring"
Expand Down Expand Up @@ -44,6 +46,12 @@ func (t Node) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of a Node instance
// which is defined by its Name.
func (t Node) String() string {
return fmt.Sprintf("Node(Name=%s)", t.Metadata.Name)
}

// NodeMetadata contains the metadata for a Calico Node resource.
type NodeMetadata struct {
unversioned.ObjectMetadata
Expand Down
8 changes: 8 additions & 0 deletions lib/api/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package api

import (
"fmt"

"github.com/projectcalico/libcalico-go/lib/api/unversioned"
)

Expand Down Expand Up @@ -46,6 +48,12 @@ func (t Policy) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of a Policy instance
// which is defined by its Name.
func (t Policy) String() string {
return fmt.Sprintf("Policy(Name=%s)", t.Metadata.Name)
}

// PolicyMetadata contains the metadata for a selector-based security Policy resource.
type PolicyMetadata struct {
unversioned.ObjectMetadata
Expand Down
8 changes: 8 additions & 0 deletions lib/api/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package api

import (
"fmt"

"github.com/projectcalico/libcalico-go/lib/api/unversioned"
)

Expand All @@ -33,6 +35,12 @@ func (t Profile) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of a Profile instance
// which is defined by its Name.
func (t Profile) String() string {
return fmt.Sprintf("Profile(Name=%s)", t.Metadata.Name)
}

// ProfileMetadata contains the metadata for a security Profile resource.
type ProfileMetadata struct {
unversioned.ObjectMetadata
Expand Down
9 changes: 8 additions & 1 deletion lib/api/unversioned/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ type Resource interface {
GetTypeMetadata() TypeMetadata
}

// All singular resources (all resources not including lists) implement the ResourceObject interface
// All singular resources (all resources not including lists) implement the ResourceObject interface.
type ResourceObject interface {
Resource

// GetResourceMetadata returns the ResourceMetadata for each Resource Object.
GetResourceMetadata() ResourceMetadata

// String returns a human-readable string representation of a ResourceObject which
// includes the important ID fields for a ResourceObject.
String() string
}

// Define available versions.
Expand All @@ -46,6 +52,7 @@ func (md TypeMetadata) GetTypeMetadata() TypeMetadata {

// All resource Metadata (not lists) implement the ResourceMetadata interface.
type ResourceMetadata interface {
// GetObjectMetadata returns the ObjectMetadata instance of the ResourceMetadata.
GetObjectMetadata() ObjectMetadata
}

Expand Down
9 changes: 9 additions & 0 deletions lib/api/workloadendpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ func (t WorkloadEndpoint) GetResourceMetadata() unversioned.ResourceMetadata {
return t.Metadata
}

// String() returns the human-readable string representation of a WorkloadEndpoint which is
// defined by its Node, Orchestrator, Workload, Name, and Active Instance ID (if it exists).
func (t WorkloadEndpoint) String() string {
if t.Metadata.ActiveInstanceID == "" {
return fmt.Sprintf("WorkloadEndpoint(Node=%s, Orchestrator=%s, Workload=%s, Name=%s)", t.Metadata.Node, t.Metadata.Orchestrator, t.Metadata.Workload, t.Metadata.Name)
}
return fmt.Sprintf("WorkloadEndpoint(Node=%s, Orchestrator=%s, Workload=%s, Name=%s, ActiveInstanceID=%s)", t.Metadata.Node, t.Metadata.Orchestrator, t.Metadata.Workload, t.Metadata.Name, t.Metadata.ActiveInstanceID)
}

// WorkloadEndpointMetadata contains the Metadata for a WorkloadEndpoint resource.
type WorkloadEndpointMetadata struct {
unversioned.ObjectMetadata
Expand Down

0 comments on commit 73b7a64

Please sign in to comment.