From 01323ece8e56d81ddf5241165b703d7884d876fd Mon Sep 17 00:00:00 2001 From: matt Date: Mon, 24 Jul 2017 13:34:28 -0700 Subject: [PATCH] Add ID fields to Resource Objects for easy lookup and validations --- lib/api/bgppeer.go | 11 +++++++++++ lib/api/hostendpoint.go | 8 ++++++++ lib/api/ippool.go | 8 ++++++++ lib/api/node.go | 8 ++++++++ lib/api/policy.go | 8 ++++++++ lib/api/profile.go | 8 ++++++++ lib/api/unversioned/types.go | 9 ++++++++- lib/api/workloadendpoint.go | 9 +++++++++ 8 files changed, 68 insertions(+), 1 deletion(-) diff --git a/lib/api/bgppeer.go b/lib/api/bgppeer.go index becb87e88..cc7bee697 100644 --- a/lib/api/bgppeer.go +++ b/lib/api/bgppeer.go @@ -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" @@ -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 diff --git a/lib/api/hostendpoint.go b/lib/api/hostendpoint.go index b7e4975ff..55bbde5b0 100644 --- a/lib/api/hostendpoint.go +++ b/lib/api/hostendpoint.go @@ -15,6 +15,8 @@ package api import ( + "fmt" + "github.com/projectcalico/libcalico-go/lib/api/unversioned" "github.com/projectcalico/libcalico-go/lib/net" ) @@ -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 diff --git a/lib/api/ippool.go b/lib/api/ippool.go index b9c138e1e..2fe2c6e2d 100644 --- a/lib/api/ippool.go +++ b/lib/api/ippool.go @@ -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" @@ -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 diff --git a/lib/api/node.go b/lib/api/node.go index 05efeacd0..55b28b907 100644 --- a/lib/api/node.go +++ b/lib/api/node.go @@ -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" @@ -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 diff --git a/lib/api/policy.go b/lib/api/policy.go index 5a8dae432..932fb6e67 100644 --- a/lib/api/policy.go +++ b/lib/api/policy.go @@ -15,6 +15,8 @@ package api import ( + "fmt" + "github.com/projectcalico/libcalico-go/lib/api/unversioned" ) @@ -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 diff --git a/lib/api/profile.go b/lib/api/profile.go index 94a25cdd6..befe8e6d3 100644 --- a/lib/api/profile.go +++ b/lib/api/profile.go @@ -15,6 +15,8 @@ package api import ( + "fmt" + "github.com/projectcalico/libcalico-go/lib/api/unversioned" ) @@ -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 diff --git a/lib/api/unversioned/types.go b/lib/api/unversioned/types.go index 655a8da19..5ef8678d1 100644 --- a/lib/api/unversioned/types.go +++ b/lib/api/unversioned/types.go @@ -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. @@ -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 } diff --git a/lib/api/workloadendpoint.go b/lib/api/workloadendpoint.go index 347cc134b..3e6a0c453 100644 --- a/lib/api/workloadendpoint.go +++ b/lib/api/workloadendpoint.go @@ -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