Example #1
0
func (s *S) TestTemplateDestroy(c *check.C) {
	iaas.RegisterIaasProvider("ec2", newTestIaaS)
	tpl1 := iaas.Template{
		Name:     "tpl1",
		IaaSName: "ec2",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "key1", Value: "val1"},
			{Name: "key2", Value: "val2"},
		}),
	}
	err := tpl1.Save()
	c.Assert(err, check.IsNil)
	defer iaas.DestroyTemplate("tpl1")
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("DELETE", "/iaas/templates/tpl1", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	templates, err := iaas.ListTemplates()
	c.Assert(err, check.IsNil)
	c.Assert(templates, check.HasLen, 0)
	c.Assert(eventtest.EventDesc{
		Target: event.Target{Type: event.TargetTypeIaas, Value: "ec2"},
		Owner:  s.token.GetUserName(),
		Kind:   "machine.template.delete",
		StartCustomData: []map[string]interface{}{
			{"name": ":template_name", "value": "tpl1"},
		},
	}, eventtest.HasEvent)
}
Example #2
0
func (c *templateUpdate) Run(context *cmd.Context, client *cmd.Client) error {
	var template iaas.Template
	template.Name = context.Args[0]
	for _, param := range context.Args[1:] {
		if strings.Contains(param, "=") {
			keyValue := strings.SplitN(param, "=", 2)
			template.Data = append(template.Data, iaas.TemplateData{
				Name:  keyValue[0],
				Value: keyValue[1],
			})
		}
	}
	templateBytes, err := json.Marshal(template)
	if err != nil {
		return err
	}
	url, err := cmd.GetURL(fmt.Sprintf("/iaas/templates/%s", template.Name))
	if err != nil {
		return err
	}
	request, err := http.NewRequest("PUT", url, bytes.NewBuffer(templateBytes))
	if err != nil {
		return err
	}
	_, err = client.Do(request)
	if err != nil {
		context.Stderr.Write([]byte("Failed to update template.\n"))
		return err
	}
	context.Stdout.Write([]byte("Template successfully updated.\n"))
	return nil
}
Example #3
0
File: iaas.go Project: tsuru/tsuru
// title: template create
// path: /iaas/templates
// method: POST
// consume: application/x-www-form-urlencoded
// responses:
//   201: Template created
//   400: Invalid data
//   401: Unauthorized
func templateCreate(w http.ResponseWriter, r *http.Request, token auth.Token) (err error) {
	err = r.ParseForm()
	if err != nil {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	var paramTemplate iaas.Template
	dec := form.NewDecoder(nil)
	dec.IgnoreUnknownKeys(true)
	err = dec.DecodeValues(&paramTemplate, r.Form)
	if err != nil {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	iaasCtx := permission.Context(permission.CtxIaaS, paramTemplate.IaaSName)
	allowed := permission.Check(token, permission.PermMachineTemplateCreate, iaasCtx)
	if !allowed {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     event.Target{Type: event.TargetTypeIaas, Value: paramTemplate.IaaSName},
		Kind:       permission.PermMachineTemplateCreate,
		Owner:      token,
		CustomData: event.FormToCustomData(r.Form),
		Allowed:    event.Allowed(permission.PermMachineReadEvents, iaasCtx),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	err = paramTemplate.Save()
	if err != nil {
		return err
	}
	w.WriteHeader(http.StatusCreated)
	return nil
}
Example #4
0
func (s *S) TestTemplateUpdate(c *check.C) {
	iaas.RegisterIaasProvider("my-iaas", newTestIaaS)
	tpl1 := iaas.Template{
		Name:     "my-tpl",
		IaaSName: "my-iaas",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "x", Value: "y"},
			{Name: "a", Value: "b"},
		}),
	}
	err := tpl1.Save()
	c.Assert(err, check.IsNil)
	tplParam := iaas.Template{
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "x", Value: ""},
			{Name: "y", Value: "8"},
			{Name: "z", Value: "9"},
		}),
	}
	v, err := form.EncodeToValues(&tplParam)
	c.Assert(err, check.IsNil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("PUT", "/iaas/templates/my-tpl", strings.NewReader(v.Encode()))
	c.Assert(err, check.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	templates, err := iaas.ListTemplates()
	c.Assert(err, check.IsNil)
	c.Assert(templates, check.HasLen, 1)
	c.Assert(templates[0].Name, check.Equals, "my-tpl")
	c.Assert(templates[0].IaaSName, check.Equals, "my-iaas")
	sort.Sort(templates[0].Data)
	c.Assert(templates[0].Data, check.DeepEquals, iaas.TemplateDataList([]iaas.TemplateData{
		{Name: "a", Value: "b"},
		{Name: "y", Value: "8"},
		{Name: "z", Value: "9"},
	}))
	c.Assert(eventtest.EventDesc{
		Target: event.Target{Type: event.TargetTypeIaas, Value: "my-iaas"},
		Owner:  s.token.GetUserName(),
		Kind:   "machine.template.update",
		StartCustomData: []map[string]interface{}{
			{"name": ":template_name", "value": "my-tpl"},
			{"name": "Data.0.Name", "value": "x"},
			{"name": "Data.0.Value", "value": ""},
			{"name": "Data.1.Name", "value": "y"},
			{"name": "Data.1.Value", "value": "8"},
			{"name": "Data.2.Name", "value": "z"},
			{"name": "Data.2.Value", "value": "9"},
		},
	}, eventtest.HasEvent)
}
Example #5
0
func templateCreate(w http.ResponseWriter, r *http.Request, token auth.Token) error {
	var paramTemplate iaas.Template
	err := json.NewDecoder(r.Body).Decode(&paramTemplate)
	if err != nil {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	err = paramTemplate.Save()
	if err != nil {
		return err
	}
	w.WriteHeader(http.StatusCreated)
	return nil
}
Example #6
0
func templateCreate(w http.ResponseWriter, r *http.Request, token auth.Token) error {
	var paramTemplate iaas.Template
	err := json.NewDecoder(r.Body).Decode(&paramTemplate)
	if err != nil {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	allowed := permission.Check(token, permission.PermMachineTemplateCreate,
		permission.Context(permission.CtxIaaS, paramTemplate.IaaSName),
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	err = paramTemplate.Save()
	if err != nil {
		return err
	}
	w.WriteHeader(http.StatusCreated)
	return nil
}
Example #7
0
func (s *S) TestTemplateUpdate(c *check.C) {
	iaas.RegisterIaasProvider("my-iaas", newTestIaaS)
	tpl1 := iaas.Template{
		Name:     "my-tpl",
		IaaSName: "my-iaas",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "x", Value: "y"},
			{Name: "a", Value: "b"},
		}),
	}
	err := tpl1.Save()
	c.Assert(err, check.IsNil)
	tplParam := iaas.Template{
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "x", Value: ""},
			{Name: "y", Value: "8"},
			{Name: "z", Value: "9"},
		}),
	}
	bodyData, err := json.Marshal(tplParam)
	c.Assert(err, check.IsNil)
	body := bytes.NewBuffer(bodyData)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("PUT", "/iaas/templates/my-tpl", body)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.admintoken.GetValue())
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	templates, err := iaas.ListTemplates()
	c.Assert(err, check.IsNil)
	c.Assert(templates, check.HasLen, 1)
	c.Assert(templates[0].Name, check.Equals, "my-tpl")
	c.Assert(templates[0].IaaSName, check.Equals, "my-iaas")
	sort.Sort(templates[0].Data)
	c.Assert(templates[0].Data, check.DeepEquals, iaas.TemplateDataList([]iaas.TemplateData{
		{Name: "a", Value: "b"},
		{Name: "y", Value: "8"},
		{Name: "z", Value: "9"},
	}))
}
Example #8
0
func (s *S) TestTemplateDestroy(c *check.C) {
	iaas.RegisterIaasProvider("ec2", newTestIaaS)
	tpl1 := iaas.Template{
		Name:     "tpl1",
		IaaSName: "ec2",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "key1", Value: "val1"},
			{Name: "key2", Value: "val2"},
		}),
	}
	err := tpl1.Save()
	c.Assert(err, check.IsNil)
	defer iaas.DestroyTemplate("tpl1")
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("DELETE", "/iaas/templates/tpl1", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.admintoken.GetValue())
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	templates, err := iaas.ListTemplates()
	c.Assert(err, check.IsNil)
	c.Assert(templates, check.HasLen, 0)
}
Example #9
0
func (s *S) TestTemplateList(c *check.C) {
	iaas.RegisterIaasProvider("ec2", newTestIaaS)
	iaas.RegisterIaasProvider("other", newTestIaaS)
	tpl1 := iaas.Template{
		Name:     "tpl1",
		IaaSName: "ec2",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "key1", Value: "val1"},
			{Name: "key2", Value: "val2"},
		}),
	}
	err := tpl1.Save()
	c.Assert(err, check.IsNil)
	defer iaas.DestroyTemplate("tpl1")
	tpl2 := iaas.Template{
		Name:     "tpl2",
		IaaSName: "other",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "key4", Value: "valX"},
			{Name: "key5", Value: "valY"},
		}),
	}
	err = tpl2.Save()
	c.Assert(err, check.IsNil)
	defer iaas.DestroyTemplate("tpl2")
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/iaas/templates", nil)
	c.Assert(err, check.IsNil)
	request.Header.Set("Authorization", "bearer "+s.token.GetValue())
	m := RunServer(true)
	m.ServeHTTP(recorder, request)
	c.Assert(recorder.Code, check.Equals, http.StatusOK)
	c.Assert(recorder.Header().Get("Content-Type"), check.Equals, "application/json")
	var templates []iaas.Template
	err = json.Unmarshal(recorder.Body.Bytes(), &templates)
	c.Assert(err, check.IsNil)
	c.Assert(templates, check.HasLen, 2)
	c.Assert(templates[0].Name, check.Equals, "tpl1")
	c.Assert(templates[1].Name, check.Equals, "tpl2")
	c.Assert(templates[0].IaaSName, check.Equals, "ec2")
	c.Assert(templates[1].IaaSName, check.Equals, "other")
	c.Assert(templates[0].Data, check.DeepEquals, iaas.TemplateDataList([]iaas.TemplateData{
		{Name: "key1", Value: "val1"},
		{Name: "key2", Value: "val2"},
	}))
	c.Assert(templates[1].Data, check.DeepEquals, iaas.TemplateDataList([]iaas.TemplateData{
		{Name: "key4", Value: "valX"},
		{Name: "key5", Value: "valY"},
	}))
}