move atom types into their own package

This commit is contained in:
David Justice 2018-06-17 13:25:54 -07:00
Родитель 416dd7499c
Коммит 635a527d81
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 2B44C6BF9F416319
9 изменённых файлов: 118 добавлений и 110 удалений

53
atom/atom.go Normal file
Просмотреть файл

@ -0,0 +1,53 @@
package atom
import (
"encoding/xml"
"github.com/Azure/go-autorest/autorest/date"
)
type (
// Feed is an Atom feed which contains entries
Feed struct {
XMLName xml.Name `xml:"feed"`
ID string `xml:"id"`
Title string `xml:"title"`
Updated *date.Time `xml:"updated,omitempty"`
Entries []Entry `xml:"entry"`
}
// Entry is the Atom wrapper for a management request
Entry struct {
XMLName xml.Name `xml:"entry"`
ID string `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
Published *date.Time `xml:"published,omitempty"`
Updated *date.Time `xml:"updated,omitempty"`
Author *Author `xml:"author,omitempty"`
Link *Link `xml:"link,omitempty"`
Content *Content `xml:"content"`
DataServiceSchema string `xml:"xmlns:d,attr,omitempty"`
DataServiceMetadataSchema string `xml:"xmlns:m,attr,omitempty"`
AtomSchema string `xml:"xmlns,attr"`
}
// Author is an Atom author used in an entry
Author struct {
XMLName xml.Name `xml:"author"`
Name *string `xml:"name,omitempty"`
}
// Link is an Atom link used in an entry
Link struct {
XMLName xml.Name `xml:"link"`
Rel string `xml:"rel,attr"`
HREF string `xml:"href,attr"`
}
// Content is a generic body for an Atom entry
Content struct {
XMLName xml.Name `xml:"content"`
Type string `xml:"type,attr"`
Body string `xml:",innerxml"`
}
)

77
mgmt.go
Просмотреть файл

@ -35,68 +35,21 @@ import (
"github.com/Azure/azure-amqp-common-go/auth"
"github.com/Azure/azure-amqp-common-go/log"
"github.com/Azure/go-autorest/autorest/date"
)
const (
serviceBusSchema = "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"
dataServiceSchema = "http://schemas.microsoft.com/ado/2007/08/dataservices"
dataServiceMetadataSchema = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
atomSchema = "http://www.w3.org/2005/Atom"
applicationXML = "application/xml"
serviceBusSchema = "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect"
atomSchema = "http://www.w3.org/2005/Atom"
applicationXML = "application/xml"
)
type (
// EntityManager provides CRUD functionality for Service Bus entities (Queues, Topics, Subscriptions...)
EntityManager struct {
// entityManager provides CRUD functionality for Service Bus entities (Queues, Topics, Subscriptions...)
entityManager struct {
TokenProvider auth.TokenProvider
Host string
}
// Feed is an Atom feed which contains entries
Feed struct {
XMLName xml.Name `xml:"feed"`
ID string `xml:"id"`
Title string `xml:"title"`
Updated *date.Time `xml:"updated,omitempty"`
Entries []Entry `xml:"entry"`
}
// Entry is the Atom wrapper for a management request
Entry struct {
XMLName xml.Name `xml:"entry"`
ID string `xml:"id,omitempty"`
Title string `xml:"title,omitempty"`
Published *date.Time `xml:"published,omitempty"`
Updated *date.Time `xml:"updated,omitempty"`
Author *Author `xml:"author,omitempty"`
Link *Link `xml:"link,omitempty"`
Content *Content `xml:"content"`
DataServiceSchema string `xml:"xmlns:d,attr,omitempty"`
DataServiceMetadataSchema string `xml:"xmlns:m,attr,omitempty"`
AtomSchema string `xml:"xmlns,attr"`
}
// Author is an Atom author used in an Entry
Author struct {
XMLName xml.Name `xml:"author"`
Name *string `xml:"name,omitempty"`
}
// Link is an Atom link used in an Entry
Link struct {
XMLName xml.Name `xml:"link"`
Rel string `xml:"rel,attr"`
HREF string `xml:"href,attr"`
}
// Content is a generic body for an Atom Entry
Content struct {
XMLName xml.Name `xml:"content"`
Type string `xml:"type,attr"`
Body string `xml:",innerxml"`
}
// BaseEntityDescription provides common fields which are part of Queues, Topics and Subscriptions
BaseEntityDescription struct {
InstanceMetadataSchema *string `xml:"xmlns:i,attr,omitempty"`
@ -114,16 +67,16 @@ func (m *managementError) String() string {
return fmt.Sprintf("Code: %d, Details: %s", m.Code, m.Detail)
}
// NewEntityManager creates a new instance of an EntityManager given a token provider and host
func NewEntityManager(host string, tokenProvider auth.TokenProvider) *EntityManager {
return &EntityManager{
// newEntityManager creates a new instance of an entityManager given a token provider and host
func newEntityManager(host string, tokenProvider auth.TokenProvider) *entityManager {
return &entityManager{
Host: host,
TokenProvider: tokenProvider,
}
}
// Get performs an HTTP Get for a given entity path
func (em *EntityManager) Get(ctx context.Context, entityPath string) (*http.Response, error) {
func (em *entityManager) Get(ctx context.Context, entityPath string) (*http.Response, error) {
span, ctx := em.startSpanFromContext(ctx, "sb.EntityManger.Get")
defer span.Finish()
@ -131,7 +84,7 @@ func (em *EntityManager) Get(ctx context.Context, entityPath string) (*http.Resp
}
// Put performs an HTTP PUT for a given entity path and body
func (em *EntityManager) Put(ctx context.Context, entityPath string, body []byte) (*http.Response, error) {
func (em *entityManager) Put(ctx context.Context, entityPath string, body []byte) (*http.Response, error) {
span, ctx := em.startSpanFromContext(ctx, "sb.EntityManger.Put")
defer span.Finish()
@ -139,7 +92,7 @@ func (em *EntityManager) Put(ctx context.Context, entityPath string, body []byte
}
// Delete performs an HTTP DELETE for a given entity path
func (em *EntityManager) Delete(ctx context.Context, entityPath string) (*http.Response, error) {
func (em *entityManager) Delete(ctx context.Context, entityPath string) (*http.Response, error) {
span, ctx := em.startSpanFromContext(ctx, "sb.EntityManger.Delete")
defer span.Finish()
@ -147,7 +100,7 @@ func (em *EntityManager) Delete(ctx context.Context, entityPath string) (*http.R
}
// Post performs an HTTP POST for a given entity path and body
func (em *EntityManager) Post(ctx context.Context, entityPath string, body []byte) (*http.Response, error) {
func (em *entityManager) Post(ctx context.Context, entityPath string, body []byte) (*http.Response, error) {
span, ctx := em.startSpanFromContext(ctx, "sb.EntityManger.Post")
defer span.Finish()
@ -155,7 +108,7 @@ func (em *EntityManager) Post(ctx context.Context, entityPath string, body []byt
}
// Execute performs an HTTP request given a http method, path and body
func (em *EntityManager) Execute(ctx context.Context, method string, entityPath string, body io.Reader) (*http.Response, error) {
func (em *entityManager) Execute(ctx context.Context, method string, entityPath string, body io.Reader) (*http.Response, error) {
span, ctx := em.startSpanFromContext(ctx, "sb.EntityManger.Execute")
defer span.Finish()
@ -190,7 +143,7 @@ func isEmptyFeed(b []byte) bool {
return feedErr == nil && emptyFeed.Title == "Publicly Listed Services"
}
func (em *EntityManager) addAuthorization(req *http.Request) (*http.Request, error) {
func (em *entityManager) addAuthorization(req *http.Request) (*http.Request, error) {
signature, err := em.TokenProvider.GetToken(req.URL.String())
if err != nil {
return nil, err
@ -202,7 +155,7 @@ func (em *EntityManager) addAuthorization(req *http.Request) (*http.Request, err
func addAtomXMLContentType(req *http.Request) *http.Request {
if req.Method != http.MethodGet && req.Method != http.MethodHead {
req.Header.Add("Content-Type", "application/atom+xml;type=entry;charset=utf-8")
req.Header.Add("content-Type", "application/atom+xml;type=entry;charset=utf-8")
}
return req
}

Просмотреть файл

@ -26,12 +26,13 @@ import (
"encoding/xml"
"time"
"github.com/Azure/azure-service-bus-go/atom"
"github.com/Azure/go-autorest/autorest/date"
"github.com/stretchr/testify/assert"
)
func (suite *serviceBusSuite) TestFeedUnmarshal() {
var feed Feed
var feed atom.Feed
err := xml.Unmarshal([]byte(feedOfQueues), &feed)
assert.Nil(suite.T(), err)
updated, err := date.ParseTime(time.RFC3339, "2018-05-03T00:21:15Z")
@ -47,7 +48,7 @@ func (suite *serviceBusSuite) TestFeedUnmarshal() {
}
func (suite *serviceBusSuite) TestEntryUnmarshal() {
var entry Entry
var entry atom.Entry
err := xml.Unmarshal([]byte(queueEntry1), &entry)
assert.Nil(suite.T(), err)
assert.Equal(suite.T(), "https://sbdjtest.servicebus.windows.net/foo", entry.ID)

Просмотреть файл

@ -33,6 +33,7 @@ import (
"github.com/Azure/azure-amqp-common-go/log"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"github.com/Azure/azure-service-bus-go/atom"
"github.com/Azure/go-autorest/autorest/date"
"github.com/Azure/go-autorest/autorest/to"
)
@ -59,7 +60,7 @@ type (
// QueueManager provides CRUD functionality for Service Bus Queues
QueueManager struct {
*EntityManager
*entityManager
}
// QueueEntity is the Azure Service Bus description of a Queue for management activities
@ -68,19 +69,19 @@ type (
Name string
}
// queueFeed is a specialized Feed containing QueueEntries
// queueFeed is a specialized feed containing QueueEntries
queueFeed struct {
*Feed
*atom.Feed
Entries []queueEntry `xml:"entry"`
}
// queueEntry is a specialized Queue Feed Entry
// queueEntry is a specialized Queue feed entry
queueEntry struct {
*Entry
*atom.Entry
Content *queueContent `xml:"content"`
}
// queueContent is a specialized Queue body for an Atom Entry
// queueContent is a specialized Queue body for an Atom entry
queueContent struct {
XMLName xml.Name `xml:"content"`
Type string `xml:"type,attr"`
@ -252,7 +253,7 @@ func QueueEntityWithMaxDeliveryCount(count int32) QueueManagementOption {
// NewQueueManager creates a new QueueManager for a Service Bus Namespace
func (ns *Namespace) NewQueueManager() *QueueManager {
return &QueueManager{
EntityManager: NewEntityManager(ns.getHTTPSHostURI(), ns.TokenProvider),
entityManager: newEntityManager(ns.getHTTPSHostURI(), ns.TokenProvider),
}
}
@ -261,7 +262,7 @@ func (qm *QueueManager) Delete(ctx context.Context, name string) error {
span, ctx := qm.startSpanFromContext(ctx, "sb.QueueManager.Delete")
defer span.Finish()
_, err := qm.EntityManager.Delete(ctx, "/"+name)
_, err := qm.entityManager.Delete(ctx, "/"+name)
return err
}
@ -281,7 +282,7 @@ func (qm *QueueManager) Put(ctx context.Context, name string, opts ...QueueManag
qd.ServiceBusSchema = to.StringPtr(serviceBusSchema)
qe := &queueEntry{
Entry: &Entry{
Entry: &atom.Entry{
AtomSchema: atomSchema,
},
Content: &queueContent{
@ -297,7 +298,7 @@ func (qm *QueueManager) Put(ctx context.Context, name string, opts ...QueueManag
}
reqBytes = xmlDoc(reqBytes)
res, err := qm.EntityManager.Put(ctx, "/"+name, reqBytes)
res, err := qm.entityManager.Put(ctx, "/"+name, reqBytes)
if err != nil {
log.For(ctx).Error(err)
return nil, err
@ -322,7 +323,7 @@ func (qm *QueueManager) List(ctx context.Context) ([]*QueueEntity, error) {
span, ctx := qm.startSpanFromContext(ctx, "sb.QueueManager.List")
defer span.Finish()
res, err := qm.EntityManager.Get(ctx, `/$Resources/Queues`)
res, err := qm.entityManager.Get(ctx, `/$Resources/Queues`)
if err != nil {
log.For(ctx).Error(err)
return nil, err
@ -352,7 +353,7 @@ func (qm *QueueManager) Get(ctx context.Context, name string) (*QueueEntity, err
span, ctx := qm.startSpanFromContext(ctx, "sb.QueueManager.Get")
defer span.Finish()
res, err := qm.EntityManager.Get(ctx, name)
res, err := qm.entityManager.Get(ctx, name)
if err != nil {
log.For(ctx).Error(err)
return nil, err

Просмотреть файл

@ -32,6 +32,7 @@ import (
"time"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2015-08-01/servicebus"
"github.com/Azure/azure-service-bus-go/atom"
"github.com/Azure/azure-service-bus-go/internal/test"
"github.com/opentracing/opentracing-go"
"github.com/stretchr/testify/assert"
@ -144,7 +145,7 @@ func (suite *serviceBusSuite) TestQueueEntryUnmarshal() {
}
func (suite *serviceBusSuite) TestQueueUnmarshal() {
var entry Entry
var entry atom.Entry
err := xml.Unmarshal([]byte(queueEntry1), &entry)
assert.Nil(suite.T(), err)

Просмотреть файл

@ -33,6 +33,7 @@ import (
"github.com/Azure/azure-amqp-common-go/log"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"github.com/Azure/azure-service-bus-go/atom"
"github.com/Azure/go-autorest/autorest/date"
"github.com/Azure/go-autorest/autorest/to"
)
@ -52,7 +53,7 @@ type (
// SubscriptionManager provides CRUD functionality for Service Bus Subscription
SubscriptionManager struct {
*EntityManager
*entityManager
Topic *Topic
}
@ -62,19 +63,19 @@ type (
Name string
}
// subscriptionFeed is a specialized Feed containing Topic Subscriptions
// subscriptionFeed is a specialized feed containing Topic Subscriptions
subscriptionFeed struct {
*Feed
*atom.Feed
Entries []subscriptionEntry `xml:"entry"`
}
// subscriptionEntryContent is a specialized Topic Feed Subscription
// subscriptionEntryContent is a specialized Topic feed Subscription
subscriptionEntry struct {
*Entry
*atom.Entry
Content *subscriptionContent `xml:"content"`
}
// subscriptionContent is a specialized Subscription body for an Atom Entry
// subscriptionContent is a specialized Subscription body for an Atom entry
subscriptionContent struct {
XMLName xml.Name `xml:"content"`
Type string `xml:"type,attr"`
@ -110,7 +111,7 @@ type (
// NewSubscriptionManager creates a new SubscriptionManager for a Service Bus Topic
func (t *Topic) NewSubscriptionManager() *SubscriptionManager {
return &SubscriptionManager{
EntityManager: NewEntityManager(t.namespace.getHTTPSHostURI(), t.namespace.TokenProvider),
entityManager: newEntityManager(t.namespace.getHTTPSHostURI(), t.namespace.TokenProvider),
Topic: t,
}
}
@ -122,7 +123,7 @@ func (ns *Namespace) NewSubscriptionManager(ctx context.Context, topicName strin
return nil, err
}
return &SubscriptionManager{
EntityManager: NewEntityManager(t.namespace.getHTTPSHostURI(), t.namespace.TokenProvider),
entityManager: newEntityManager(t.namespace.getHTTPSHostURI(), t.namespace.TokenProvider),
Topic: t,
}, nil
}
@ -132,7 +133,7 @@ func (sm *SubscriptionManager) Delete(ctx context.Context, name string) error {
span, ctx := sm.startSpanFromContext(ctx, "sb.SubscriptionManager.Delete")
defer span.Finish()
_, err := sm.EntityManager.Delete(ctx, sm.getResourceURI(name))
_, err := sm.entityManager.Delete(ctx, sm.getResourceURI(name))
return err
}
@ -151,10 +152,8 @@ func (sm *SubscriptionManager) Put(ctx context.Context, name string, opts ...Sub
sd.ServiceBusSchema = to.StringPtr(serviceBusSchema)
qe := &subscriptionEntry{
Entry: &Entry{
DataServiceSchema: dataServiceSchema,
DataServiceMetadataSchema: dataServiceMetadataSchema,
AtomSchema: atomSchema,
Entry: &atom.Entry{
AtomSchema: atomSchema,
},
Content: &subscriptionContent{
Type: applicationXML,
@ -168,7 +167,7 @@ func (sm *SubscriptionManager) Put(ctx context.Context, name string, opts ...Sub
}
reqBytes = xmlDoc(reqBytes)
res, err := sm.EntityManager.Put(ctx, sm.getResourceURI(name), reqBytes)
res, err := sm.entityManager.Put(ctx, sm.getResourceURI(name), reqBytes)
if err != nil {
return nil, err
}
@ -191,7 +190,7 @@ func (sm *SubscriptionManager) List(ctx context.Context) ([]*SubscriptionEntity,
span, ctx := sm.startSpanFromContext(ctx, "sb.SubscriptionManager.List")
defer span.Finish()
res, err := sm.EntityManager.Get(ctx, "/"+sm.Topic.Name+"/subscriptions")
res, err := sm.entityManager.Get(ctx, "/"+sm.Topic.Name+"/subscriptions")
if err != nil {
return nil, err
}
@ -219,7 +218,7 @@ func (sm *SubscriptionManager) Get(ctx context.Context, name string) (*Subscript
span, ctx := sm.startSpanFromContext(ctx, "sb.SubscriptionManager.Get")
defer span.Finish()
res, err := sm.EntityManager.Get(ctx, sm.getResourceURI(name))
res, err := sm.entityManager.Get(ctx, sm.getResourceURI(name))
if err != nil {
return nil, err
}

Просмотреть файл

@ -33,6 +33,7 @@ import (
"github.com/Azure/azure-amqp-common-go/log"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"github.com/Azure/azure-service-bus-go/atom"
"github.com/Azure/go-autorest/autorest/date"
"github.com/Azure/go-autorest/autorest/to"
)
@ -55,7 +56,7 @@ type (
// TopicManager provides CRUD functionality for Service Bus Topics
TopicManager struct {
*EntityManager
*entityManager
}
// TopicEntity is the Azure Service Bus description of a Topic for management activities
@ -64,19 +65,19 @@ type (
Name string
}
// topicFeed is a specialized Feed containing Topic Entries
// topicFeed is a specialized feed containing Topic Entries
topicFeed struct {
*Feed
*atom.Feed
Entries []topicEntry `xml:"entry"`
}
// topicEntry is a specialized Topic Feed Entry
// topicEntry is a specialized Topic feed entry
topicEntry struct {
*Entry
*atom.Entry
Content *topicContent `xml:"content"`
}
// topicContent is a specialized Topic body for an Atom Entry
// topicContent is a specialized Topic body for an Atom entry
topicContent struct {
XMLName xml.Name `xml:"content"`
Type string `xml:"type,attr"`
@ -115,7 +116,7 @@ type (
// NewTopicManager creates a new TopicManager for a Service Bus Namespace
func (ns *Namespace) NewTopicManager() *TopicManager {
return &TopicManager{
EntityManager: NewEntityManager(ns.getHTTPSHostURI(), ns.TokenProvider),
entityManager: newEntityManager(ns.getHTTPSHostURI(), ns.TokenProvider),
}
}
@ -124,7 +125,7 @@ func (tm *TopicManager) Delete(ctx context.Context, name string) error {
span, ctx := tm.startSpanFromContext(ctx, "sb.TopicManager.Delete")
defer span.Finish()
_, err := tm.EntityManager.Delete(ctx, "/"+name)
_, err := tm.entityManager.Delete(ctx, "/"+name)
return err
}
@ -144,10 +145,8 @@ func (tm *TopicManager) Put(ctx context.Context, name string, opts ...TopicManag
td.ServiceBusSchema = to.StringPtr(serviceBusSchema)
qe := &topicEntry{
Entry: &Entry{
DataServiceSchema: dataServiceSchema,
DataServiceMetadataSchema: dataServiceMetadataSchema,
AtomSchema: atomSchema,
Entry: &atom.Entry{
AtomSchema: atomSchema,
},
Content: &topicContent{
Type: applicationXML,
@ -162,7 +161,7 @@ func (tm *TopicManager) Put(ctx context.Context, name string, opts ...TopicManag
}
reqBytes = xmlDoc(reqBytes)
res, err := tm.EntityManager.Put(ctx, "/"+name, reqBytes)
res, err := tm.entityManager.Put(ctx, "/"+name, reqBytes)
if err != nil {
log.For(ctx).Error(err)
return nil, err
@ -187,7 +186,7 @@ func (tm *TopicManager) List(ctx context.Context) ([]*TopicEntity, error) {
span, ctx := tm.startSpanFromContext(ctx, "sb.TopicManager.List")
defer span.Finish()
res, err := tm.EntityManager.Get(ctx, `/$Resources/Topics`)
res, err := tm.entityManager.Get(ctx, `/$Resources/Topics`)
if err != nil {
log.For(ctx).Error(err)
return nil, err
@ -217,7 +216,7 @@ func (tm *TopicManager) Get(ctx context.Context, name string) (*TopicEntity, err
span, ctx := tm.startSpanFromContext(ctx, "sb.TopicManager.Get")
defer span.Finish()
res, err := tm.EntityManager.Get(ctx, name)
res, err := tm.entityManager.Get(ctx, name)
if err != nil {
log.For(ctx).Error(err)
return nil, err

Просмотреть файл

@ -30,6 +30,7 @@ import (
"time"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2015-08-01/servicebus"
"github.com/Azure/azure-service-bus-go/atom"
"github.com/stretchr/testify/assert"
)
@ -87,7 +88,7 @@ func (suite *serviceBusSuite) TestTopicEntryUnmarshal() {
}
func (suite *serviceBusSuite) TestTopicUnmarshal() {
var entry Entry
var entry atom.Entry
err := xml.Unmarshal([]byte(topicEntry1), &entry)
suite.Nil(err)

Просмотреть файл

@ -50,7 +50,7 @@ func (m *Message) startSpanFromContext(ctx context.Context, operationName string
return span, ctx
}
func (em *EntityManager) startSpanFromContext(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
func (em *entityManager) startSpanFromContext(ctx context.Context, operationName string, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
span, ctx := opentracing.StartSpanFromContext(ctx, operationName, opts...)
applyComponentInfo(span)
tag.SpanKindRPCClient.Set(span)