Beispiel #1
0
func (s *UnitFacadeSuite) TestGetResourceInfoOkay(c *gc.C) {
	opened1 := resourcetesting.NewResource(c, s.stub, "spam", "a-application", "some data")
	res1 := opened1.Resource
	opened2 := resourcetesting.NewResource(c, s.stub, "eggs", "a-application", "other data")
	res2 := opened2.Resource
	store := &stubUnitDataStore{Stub: s.stub}
	store.ReturnListResources = resource.ServiceResources{
		Resources: []resource.Resource{res1, res2},
	}
	uf := server.UnitFacade{DataStore: store}

	results, err := uf.GetResourceInfo(private.ListResourcesArgs{
		ResourceNames: []string{"spam", "eggs"},
	})
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "ListResources")
	c.Check(results, jc.DeepEquals, private.ResourcesResult{
		Resources: []private.ResourceResult{{
			Resource: api.Resource2API(res1),
		}, {
			Resource: api.Resource2API(res2),
		}},
	})
}
Beispiel #2
0
// HandleRequest handles a resource upload request.
func (uh UploadHandler) HandleRequest(req *http.Request) (*api.UploadResult, error) {
	defer req.Body.Close()

	uploaded, err := uh.ReadResource(req)
	if err != nil {
		return nil, errors.Trace(err)
	}

	var stored resource.Resource
	if uploaded.PendingID != "" {
		stored, err = uh.Store.UpdatePendingResource(uploaded.Service, uploaded.PendingID, uh.Username, uploaded.Resource, uploaded.Data)
		if err != nil {
			return nil, errors.Trace(err)
		}
	} else {
		stored, err = uh.Store.SetResource(uploaded.Service, uh.Username, uploaded.Resource, uploaded.Data)
		if err != nil {
			return nil, errors.Trace(err)
		}
	}

	result := &api.UploadResult{
		Resource: api.Resource2API(stored),
	}
	return result, nil
}
Beispiel #3
0
func (s *UploadSuite) TestHandleRequestPending(c *gc.C) {
	content := "<some data>"
	res, _ := newResource(c, "spam", "a-user", content)
	res.PendingID = "some-unique-id"
	stored, _ := newResource(c, "spam", "", "")
	stored.PendingID = "some-unique-id"
	s.data.ReturnGetPendingResource = stored
	s.data.ReturnUpdatePendingResource = res
	uh := server.UploadHandler{
		Username: "******",
		Store:    s.data,
	}
	req, body := newUploadRequest(c, "spam", "a-service", content)
	req.URL.RawQuery += "&pendingid=some-unique-id"

	result, err := uh.HandleRequest(req)
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "GetPendingResource", "UpdatePendingResource")
	s.stub.CheckCall(c, 0, "GetPendingResource", "a-service", "spam", "some-unique-id")
	s.stub.CheckCall(c, 1, "UpdatePendingResource", "a-service", "some-unique-id", "a-user", res.Resource, ioutil.NopCloser(body))
	c.Check(result, jc.DeepEquals, &api.UploadResult{
		Resource: api.Resource2API(res),
	})
}
Beispiel #4
0
func (s *stubAPI) setResource(info resource.Resource, reader io.ReadCloser) {
	s.ReturnFacadeCall = private.ResourcesResult{
		Resources: []private.ResourceResult{{
			Resource: api.Resource2API(info),
		}},
	}
	s.ReturnDo = &http.Response{
		Body: reader,
	}
}
Beispiel #5
0
// ListResources returns the list of resources for the given service.
func (f Facade) ListResources(args api.ListResourcesArgs) (api.ResourcesResults, error) {
	var r api.ResourcesResults
	r.Results = make([]api.ResourcesResult, len(args.Entities))

	for i, e := range args.Entities {
		logger.Tracef("Listing resources for %q", e.Tag)
		tag, apierr := parseServiceTag(e.Tag)
		if apierr != nil {
			r.Results[i] = api.ResourcesResult{
				ErrorResult: params.ErrorResult{
					Error: apierr,
				},
			}
			continue
		}

		svcRes, err := f.store.ListResources(tag.Id())
		if err != nil {
			r.Results[i] = errorResult(err)
			continue
		}

		var result api.ResourcesResult
		for _, res := range svcRes.Resources {
			result.Resources = append(result.Resources, api.Resource2API(res))
		}
		for _, unitRes := range svcRes.UnitResources {
			unit := api.UnitResources{
				Entity: params.Entity{Tag: unitRes.Tag.String()},
			}
			for _, res := range unitRes.Resources {
				unit.Resources = append(unit.Resources, api.Resource2API(res))
			}
			result.UnitResources = append(result.UnitResources, unit)
		}
		r.Results[i] = result
	}
	return r, nil
}
Beispiel #6
0
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-service/spam",
		PendingID: "some-unique-ID",
		ServiceID: "a-service",
		Username:  "******",
		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-service/spam",
		PendingID: "some-unique-ID",
		ServiceID: "a-service",
		Username:  "******",
		Timestamp: now,
	})
}
Beispiel #7
0
// GetResourceInfo returns the resource info for each of the given
// resource names (for the implicit service). If any one is missing then
// the corresponding result is set with errors.NotFound.
func (uf UnitFacade) GetResourceInfo(args private.ListResourcesArgs) (private.ResourcesResult, error) {
	var r private.ResourcesResult
	r.Resources = make([]private.ResourceResult, len(args.ResourceNames))

	resources, err := uf.DataStore.ListResources()
	if err != nil {
		r.Error = common.ServerError(err)
		return r, nil
	}

	for i, name := range args.ResourceNames {
		res, ok := lookUpResource(name, resources.Resources)
		if !ok {
			r.Resources[i].Error = common.ServerError(errors.NotFoundf("resource %q", name))
			continue
		}

		r.Resources[i].Resource = api.Resource2API(res)
	}
	return r, nil
}
Beispiel #8
0
func (s *UploadSuite) TestHandleRequestOkay(c *gc.C) {
	content := "<some data>"
	res, _ := newResource(c, "spam", "a-user", content)
	stored, _ := newResource(c, "spam", "", "")
	s.data.ReturnGetResource = stored
	s.data.ReturnSetResource = res
	uh := server.UploadHandler{
		Username: "******",
		Store:    s.data,
	}
	req, body := newUploadRequest(c, "spam", "a-service", content)

	result, err := uh.HandleRequest(req)
	c.Assert(err, jc.ErrorIsNil)

	s.stub.CheckCallNames(c, "GetResource", "SetResource")
	s.stub.CheckCall(c, 0, "GetResource", "a-service", "spam")
	s.stub.CheckCall(c, 1, "SetResource", "a-service", "a-user", res.Resource, ioutil.NopCloser(body))
	c.Check(result, jc.DeepEquals, &api.UploadResult{
		Resource: api.Resource2API(res),
	})
}
Beispiel #9
0
func (HelpersSuite) TestServiceResources2API(c *gc.C) {
	res1 := resourcetesting.NewResource(c, nil, "res1", "a-service", "data").Resource
	res2 := resourcetesting.NewResource(c, nil, "res2", "a-service", "data2").Resource

	tag0 := names.NewUnitTag("a-service/0")
	tag1 := names.NewUnitTag("a-service/1")

	chres1 := res1.Resource
	chres2 := res2.Resource
	chres1.Revision++
	chres2.Revision++

	svcRes := resource.ServiceResources{
		Resources: []resource.Resource{
			res1,
			res2,
		},
		UnitResources: []resource.UnitResources{
			{
				Tag: tag0,
				Resources: []resource.Resource{
					res1,
					res2,
				},
				DownloadProgress: map[string]int64{
					res2.Name: 2,
				},
			},
			{
				Tag: tag1,
			},
		},
		CharmStoreResources: []charmresource.Resource{
			chres1,
			chres2,
		},
	}

	result := api.ServiceResources2APIResult(svcRes)

	apiRes1 := api.Resource2API(res1)
	apiRes2 := api.Resource2API(res2)

	apiChRes1 := api.CharmResource2API(chres1)
	apiChRes2 := api.CharmResource2API(chres2)

	c.Check(result, jc.DeepEquals, api.ResourcesResult{
		Resources: []api.Resource{
			apiRes1,
			apiRes2,
		},
		UnitResources: []api.UnitResources{
			{
				Entity: params.Entity{
					Tag: "unit-a-service-0",
				},
				Resources: []api.Resource{
					apiRes1,
					apiRes2,
				},
				DownloadProgress: map[string]int64{
					res2.Name: 2,
				},
			},
			{
				// we should have a listing for every unit, even if they
				// have no resources.
				Entity: params.Entity{
					Tag: "unit-a-service-1",
				},
			},
		},
		CharmStoreResources: []api.CharmResource{
			apiChRes1,
			apiChRes2,
		},
	})
}