本文整理汇总了Golang中gopkg/in/juju/charm/v6-unstable/resource.Resource类的典型用法代码示例。如果您正苦于以下问题:Golang Resource类的具体用法?Golang Resource怎么用?Golang Resource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Resource类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestCharmResource2API
func (helpersSuite) TestCharmResource2API(c *gc.C) {
fp, err := charmresource.NewFingerprint([]byte(fingerprint))
c.Assert(err, jc.ErrorIsNil)
res := 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,
}
err = res.Validate()
c.Assert(err, jc.ErrorIsNil)
apiInfo := api.CharmResource2API(res)
c.Check(apiInfo, jc.DeepEquals, api.CharmResource{
Name: "spam",
Type: "file",
Path: "spam.tgz",
Description: "you need it",
Origin: "upload",
Revision: 1,
Fingerprint: []byte(fingerprint),
Size: 10,
})
}
示例2: TestAPI2CharmResource
func (helpersSuite) TestAPI2CharmResource(c *gc.C) {
res, err := api.API2CharmResource(api.CharmResource{
Name: "spam",
Type: "file",
Path: "spam.tgz",
Description: "you need it",
Origin: "upload",
Revision: 1,
Fingerprint: []byte(fingerprint),
Size: 10,
})
c.Assert(err, jc.ErrorIsNil)
fp, err := charmresource.NewFingerprint([]byte(fingerprint))
c.Assert(err, jc.ErrorIsNil)
expected := 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,
}
err = expected.Validate()
c.Assert(err, jc.ErrorIsNil)
c.Check(res, jc.DeepEquals, expected)
}
示例3: SetCharmStoreResource
// SetCharmStoreResource stores the resource info that was retrieved
// from the charm store.
func (p ResourcePersistence) SetCharmStoreResource(id, serviceID string, res charmresource.Resource, lastPolled time.Time) error {
if err := res.Validate(); err != nil {
return errors.Annotate(err, "bad resource")
}
csRes := charmStoreResource{
Resource: res,
id: id,
serviceID: serviceID,
lastPolled: lastPolled,
}
buildTxn := func(attempt int) ([]txn.Op, error) {
// This is an "upsert".
var ops []txn.Op
switch attempt {
case 0:
ops = newInsertCharmStoreResourceOps(csRes)
case 1:
ops = newUpdateCharmStoreResourceOps(csRes)
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(serviceID)...)
return ops, nil
}
if err := p.base.Run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
}
示例4: 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.*`)
}
示例5: NewCharmResource
// NewCharmResource produces basic resource info for the given name
// and content. The origin is set set to "upload".
func NewCharmResource(c *gc.C, name, content string) charmresource.Resource {
fp, err := charmresource.GenerateFingerprint(strings.NewReader(content))
c.Assert(err, jc.ErrorIsNil)
res := charmresource.Resource{
Meta: charmresource.Meta{
Name: name,
Type: charmresource.TypeFile,
Path: name + ".tgz",
},
Origin: charmresource.OriginUpload,
Revision: 0,
Fingerprint: fp,
Size: int64(len(content)),
}
err = res.Validate()
c.Assert(err, jc.ErrorIsNil)
return res
}
示例6: API2CharmResource
// API2CharmResource converts an API CharmResource struct into
// a charm resource.
func API2CharmResource(apiInfo CharmResource) (charmresource.Resource, error) {
var res charmresource.Resource
rtype, err := charmresource.ParseType(apiInfo.Type)
if err != nil {
return res, errors.Trace(err)
}
origin, err := charmresource.ParseOrigin(apiInfo.Origin)
if err != nil {
return res, errors.Trace(err)
}
fp, err := resource.DeserializeFingerprint(apiInfo.Fingerprint)
if err != nil {
return res, errors.Trace(err)
}
res = charmresource.Resource{
Meta: charmresource.Meta{
Name: apiInfo.Name,
Type: rtype,
Path: apiInfo.Path,
Description: apiInfo.Description,
},
Origin: origin,
Revision: apiInfo.Revision,
Fingerprint: fp,
Size: apiInfo.Size,
}
if err := res.Validate(); err != nil {
return res, errors.Trace(err)
}
return res, nil
}
示例7: charmRes
func charmRes(c *gc.C, name, suffix, description, content string) charmresource.Resource {
if content == "" {
content = name
}
fp, err := charmresource.GenerateFingerprint(strings.NewReader(content))
c.Assert(err, jc.ErrorIsNil)
res := charmresource.Resource{
Meta: charmresource.Meta{
Name: name,
Type: charmresource.TypeFile,
Path: name + suffix,
Description: description,
},
Origin: charmresource.OriginStore,
Revision: 1,
Fingerprint: fp,
Size: int64(len(content)),
}
err = res.Validate()
c.Assert(err, jc.ErrorIsNil)
return res
}
示例8: updateResource
// updateResource returns a copy of the provided resource, updated with
// the given information.
func (uh UploadHandler) updateResource(res charmresource.Resource, fp charmresource.Fingerprint, size int64) (charmresource.Resource, error) {
res.Origin = charmresource.OriginUpload
res.Revision = 0
res.Fingerprint = fp
res.Size = size
if err := res.Validate(); err != nil {
return res, errors.Trace(err)
}
return res, nil
}