Ejemplo n.º 1
0
func (s *S) TestTemplateCreate(c *check.C) {
	iaas.RegisterIaasProvider("my-iaas", newTestIaaS)
	data := iaas.Template{
		Name:     "my-tpl",
		IaaSName: "my-iaas",
		Data: iaas.TemplateDataList([]iaas.TemplateData{
			{Name: "x", Value: "y"},
			{Name: "a", Value: "b"},
		}),
	}
	bodyData, err := json.Marshal(data)
	c.Assert(err, check.IsNil)
	body := bytes.NewBuffer(bodyData)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("POST", "/iaas/templates", 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.StatusCreated)
	defer iaas.DestroyTemplate("my-tpl")
	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")
	c.Assert(templates[0].Data, check.DeepEquals, iaas.TemplateDataList([]iaas.TemplateData{
		{Name: "x", Value: "y"},
		{Name: "a", Value: "b"},
	}))
}
Ejemplo n.º 2
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)
}
Ejemplo n.º 3
0
func templatesList(w http.ResponseWriter, r *http.Request, token auth.Token) error {
	templates, err := iaas.ListTemplates()
	if err != nil {
		return err
	}
	return json.NewEncoder(w).Encode(templates)
}
Ejemplo n.º 4
0
Archivo: iaas.go Proyecto: tsuru/tsuru
// title: machine template list
// path: /iaas/templates
// method: GET
// produce: application/json
// responses:
//   200: OK
//   401: Unauthorized
func templatesList(w http.ResponseWriter, r *http.Request, token auth.Token) error {
	templates, err := iaas.ListTemplates()
	if err != nil {
		return err
	}
	contexts := permission.ContextsForPermission(token, permission.PermMachineTemplateRead)
	allowedIaaS := map[string]struct{}{}
	for _, c := range contexts {
		if c.CtxType == permission.CtxGlobal {
			allowedIaaS = nil
			break
		}
		if c.CtxType == permission.CtxIaaS {
			allowedIaaS[c.Value] = struct{}{}
		}
	}
	for i := 0; allowedIaaS != nil && i < len(templates); i++ {
		if _, ok := allowedIaaS[templates[i].IaaSName]; !ok {
			templates = append(templates[:i], templates[i+1:]...)
			i--
		}
	}
	w.Header().Add("Content-Type", "application/json")
	return json.NewEncoder(w).Encode(templates)
}
Ejemplo n.º 5
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)
}
Ejemplo n.º 6
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.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"},
	}))
}
Ejemplo n.º 7
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)
}