本文整理汇总了Golang中github.com/juju/juju/resource.Resource类的典型用法代码示例。如果您正苦于以下问题:Golang Resource类的具体用法?Golang Resource怎么用?Golang Resource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Resource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setUnitResource
func (p ResourcePersistence) setUnitResource(unitID string, res resource.Resource, progress *int64) error {
stored, err := p.getStored(res)
if err != nil {
return errors.Trace(err)
}
// TODO(ericsnow) Ensure that stored.Resource matches res? If we do
// so then the following line is unnecessary.
stored.Resource = res
if err := res.Validate(); err != nil {
return errors.Annotate(err, "bad resource")
}
buildTxn := func(attempt int) ([]txn.Op, error) {
// This is an "upsert".
var ops []txn.Op
switch attempt {
case 0:
ops = newInsertUnitResourceOps(unitID, stored, progress)
case 1:
ops = newUpdateUnitResourceOps(unitID, stored, progress)
default:
// Either insert or update will work so we should not get here.
return nil, errors.New("setting the resource failed")
}
// No pending resources so we always do this here.
ops = append(ops, p.base.ServiceExistsOps(res.ServiceID)...)
return ops, nil
}
if err := p.base.Run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
}
示例2: setResource
func (st resourceState) setResource(pendingID, applicationID, userID string, chRes charmresource.Resource, r io.Reader) (resource.Resource, error) {
id := newResourceID(applicationID, chRes.Name)
res := resource.Resource{
Resource: chRes,
ID: id,
PendingID: pendingID,
ApplicationID: applicationID,
}
if r != nil {
// TODO(ericsnow) Validate the user ID (or use a tag).
res.Username = userID
res.Timestamp = st.currentTimestamp()
}
if err := res.Validate(); err != nil {
return res, errors.Annotate(err, "bad resource metadata")
}
if r == nil {
if err := st.persist.SetResource(res); err != nil {
return res, errors.Trace(err)
}
} else {
if err := st.storeResource(res, r); err != nil {
return res, errors.Trace(err)
}
}
return res, nil
}
示例3: TestValidateZeroValue
func (ResourceSuite) TestValidateZeroValue(c *gc.C) {
var res resource.Resource
err := res.Validate()
c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `.*bad info.*`)
}
示例4: TestValidateUploadNotUsed
func (ResourceSuite) TestValidateUploadNotUsed(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ID: "a-service/spam",
ServiceID: "a-service",
}
err := res.Validate()
c.Check(err, jc.ErrorIsNil)
}
示例5: TestValidateUploadNotUsed
func (ResourceSuite) TestValidateUploadNotUsed(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ID: "a-application/spam",
ApplicationID: "a-application",
}
err := res.Validate()
c.Check(err, jc.ErrorIsNil)
}
示例6: TestValidateMissingID
func (ResourceSuite) TestValidateMissingID(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ServiceID: "a-service",
Username: "a-user",
Timestamp: time.Now(),
}
err := res.Validate()
c.Check(err, jc.ErrorIsNil)
}
示例7: TestValidateMissingServiceID
func (ResourceSuite) TestValidateMissingServiceID(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ID: "a-service/spam",
Username: "a-user",
Timestamp: time.Now(),
}
err := res.Validate()
c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `.*missing service ID.*`)
}
示例8: TestValidateUploadUsed
func (ResourceSuite) TestValidateUploadUsed(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ID: "a-application/spam",
ApplicationID: "a-application",
Username: "a-user",
Timestamp: time.Now(),
}
err := res.Validate()
c.Check(err, jc.ErrorIsNil)
}
示例9: TestValidateBadInfo
func (ResourceSuite) TestValidateBadInfo(c *gc.C) {
var charmRes charmresource.Resource
c.Assert(charmRes.Validate(), gc.NotNil)
res := resource.Resource{
Resource: charmRes,
}
err := res.Validate()
c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `.*bad info.*`)
}
示例10: TestValidateUploadPending
func (ResourceSuite) TestValidateUploadPending(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ID: "a-service/spam",
PendingID: "some-unique-ID",
ServiceID: "a-service",
Username: "a-user",
Timestamp: time.Now(),
}
err := res.Validate()
c.Check(err, jc.ErrorIsNil)
}
示例11: TestValidateMissingTimestamp
func (ResourceSuite) TestValidateMissingTimestamp(c *gc.C) {
res := resource.Resource{
Resource: newFullCharmResource(c, "spam"),
ID: "a-application/spam",
ApplicationID: "a-application",
Username: "a-user",
Timestamp: time.Time{},
}
err := res.Validate()
c.Check(errors.Cause(err), jc.Satisfies, errors.IsNotValid)
c.Check(err, gc.ErrorMatches, `.*missing timestamp.*`)
}
示例12: TestAPI2Resource
func (helpersSuite) TestAPI2Resource(c *gc.C) {
now := time.Now()
res, err := api.API2Resource(api.Resource{
CharmResource: api.CharmResource{
Name: "spam",
Type: "file",
Path: "spam.tgz",
Description: "you need it",
Origin: "upload",
Revision: 1,
Fingerprint: []byte(fingerprint),
Size: 10,
},
ID: "a-service/spam",
PendingID: "some-unique-ID",
ServiceID: "a-service",
Username: "a-user",
Timestamp: now,
})
c.Assert(err, jc.ErrorIsNil)
fp, err := charmresource.NewFingerprint([]byte(fingerprint))
c.Assert(err, jc.ErrorIsNil)
expected := resource.Resource{
Resource: charmresource.Resource{
Meta: charmresource.Meta{
Name: "spam",
Type: charmresource.TypeFile,
Path: "spam.tgz",
Description: "you need it",
},
Origin: charmresource.OriginUpload,
Revision: 1,
Fingerprint: fp,
Size: 10,
},
ID: "a-service/spam",
PendingID: "some-unique-ID",
ServiceID: "a-service",
Username: "a-user",
Timestamp: now,
}
err = expected.Validate()
c.Assert(err, jc.ErrorIsNil)
c.Check(res, jc.DeepEquals, expected)
}
示例13: newResource
func newResource(c *gc.C, name, serviceID, username, content string) resource.Resource {
var timestamp time.Time
if username != "" {
timestamp = time.Now().UTC()
}
res := resource.Resource{
Resource: NewCharmResource(c, name, content),
ID: serviceID + "/" + name,
PendingID: "",
ServiceID: serviceID,
Username: username,
Timestamp: timestamp,
}
err := res.Validate()
c.Assert(err, jc.ErrorIsNil)
return res
}
示例14: TestResource2API
func (HelpersSuite) TestResource2API(c *gc.C) {
fp, err := charmresource.NewFingerprint([]byte(fingerprint))
c.Assert(err, jc.ErrorIsNil)
now := time.Now()
res := resource.Resource{
Resource: charmresource.Resource{
Meta: charmresource.Meta{
Name: "spam",
Type: charmresource.TypeFile,
Path: "spam.tgz",
Description: "you need it",
},
Origin: charmresource.OriginUpload,
Revision: 1,
Fingerprint: fp,
Size: 10,
},
ID: "a-application/spam",
PendingID: "some-unique-ID",
ApplicationID: "a-application",
Username: "a-user",
Timestamp: now,
}
err = res.Validate()
c.Assert(err, jc.ErrorIsNil)
apiRes := api.Resource2API(res)
c.Check(apiRes, jc.DeepEquals, api.Resource{
CharmResource: api.CharmResource{
Name: "spam",
Type: "file",
Path: "spam.tgz",
Description: "you need it",
Origin: "upload",
Revision: 1,
Fingerprint: []byte(fingerprint),
Size: 10,
},
ID: "a-application/spam",
PendingID: "some-unique-ID",
ApplicationID: "a-application",
Username: "a-user",
Timestamp: now,
})
}
示例15: newResource
func newResource(c *gc.C, name, applicationID, username, content string) resource.Resource {
var timestamp time.Time
if username != "" {
// TODO(perrito666) 2016-05-02 lp:1558657
timestamp = time.Now().UTC()
}
res := resource.Resource{
Resource: NewCharmResource(c, name, content),
ID: applicationID + "/" + name,
PendingID: "",
ApplicationID: applicationID,
Username: username,
Timestamp: timestamp,
}
err := res.Validate()
c.Assert(err, jc.ErrorIsNil)
return res
}