// SetupTest will bootstrap and provide the testing environment func (suite *FactorsTestSuite) SetupTest() { // Connect to mongo coredb session, err := mongo.OpenSession(suite.cfg.MongoDB) defer mongo.CloseSession(session) if err != nil { panic(err) } // Add authentication token to mongo coredb seedAuth := bson.M{"name": "TEST", "db_conf": []bson.M{bson.M{"server": "127.0.0.1", "port": 27017, "database": "AR_test"}}, "users": []bson.M{bson.M{"name": "Jack Doe", "email": "*****@*****.**", "api_key": "secret", "roles": []string{"viewer"}}}} _ = mongo.Insert(session, suite.cfg.MongoDB.Db, "tenants", seedAuth) // Add a few factors in collection c := session.DB(suite.tenantcfg.Db).C("weights") c.Insert(bson.M{"hepspec": 14595, "name": "CIEMAT-LCG2"}) c.Insert(bson.M{"hepspec": 1019, "name": "CFP-IST"}) c.Insert(bson.M{"hepspec": 5406, "name": "CETA-GRID"}) c = session.DB(suite.cfg.MongoDB.Db).C("roles") c.Insert( bson.M{ "resource": "factors.list", "roles": []string{"editor", "viewer"}, }) }
//Create a new metric profile func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusOK h := http.Header{} output := []byte("") err := error(nil) charset := "utf-8" //STANDARD DECLARATIONS END // Set Content-Type response Header value contentType := r.Header.Get("Accept") h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) // Grab Tenant DB configuration from context tenantDbConfig := context.Get(r, "tenant_conf").(config.MongoConfig) session, err := mongo.OpenSession(tenantDbConfig) defer mongo.CloseSession(session) if err != nil { code = http.StatusInternalServerError return code, h, output, err } incoming := MongoInterface{} // Try ingest request body body, err := ioutil.ReadAll(io.LimitReader(r.Body, cfg.Server.ReqSizeLimit)) if err != nil { panic(err) } if err := r.Body.Close(); err != nil { panic(err) } // Parse body json if err := json.Unmarshal(body, &incoming); err != nil { output, _ = respond.MarshalContent(respond.BadRequestBadJSON, contentType, "", " ") code = 400 return code, h, output, err } // Generate new id incoming.ID = mongo.NewUUID() err = mongo.Insert(session, tenantDbConfig.Db, "metric_profiles", incoming) if err != nil { panic(err) } // Create view of the results output, err = createRefView(incoming, "Metric Profile successfully created", 201, r) //Render the results into JSON code = 201 return code, h, output, err }
// Setup the Test Environment // This function runs before any test and setups the environment // A test configuration object is instantiated using a reference // to testdb: argo_test_details. Also here is are instantiated some expected // xml response validation messages (authorization,crud responses). // Also the testdb is seeded with tenants,reports,metric_profiles and status_metrics func (suite *StatusEndpointGroupsTestSuite) SetupTest() { const testConfig = ` [server] bindip = "" port = 8080 maxprocs = 4 cache = false lrucache = 700000000 gzip = true [mongodb] host = "127.0.0.1" port = 27017 db = "argotest_egroups" ` _ = gcfg.ReadStringInto(&suite.cfg, testConfig) // Create router and confhandler for test suite.confHandler = respond.ConfHandler{suite.cfg} suite.router = mux.NewRouter().StrictSlash(true).PathPrefix("/api/v2/status").Subrouter() HandleSubrouter(suite.router, &suite.confHandler) // Connect to mongo testdb session, _ := mongo.OpenSession(suite.cfg.MongoDB) // Add authentication token to mongo testdb seedAuth := bson.M{"api_key": "S3CR3T"} _ = mongo.Insert(session, suite.cfg.MongoDB.Db, "authentication", seedAuth) // seed mongo session, err := mgo.Dial(suite.cfg.MongoDB.Host) if err != nil { panic(err) } defer session.Close() // seed a tenant to use c := session.DB(suite.cfg.MongoDB.Db).C("tenants") c.Insert(bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50c", "info": bson.M{ "name": "GUARDIANS", "email": "email@something2", "website": "www.gotg.com", "created": "2015-10-20 02:08:04", "updated": "2015-10-20 02:08:04"}, "db_conf": []bson.M{ bson.M{ "store": "main", "server": "localhost", "port": 27017, "database": "argotest_egroups_egi", "username": "", "password": ""}, }, "users": []bson.M{ bson.M{ "name": "egi_user", "email": "*****@*****.**", "api_key": "KEY1"}, }}) c.Insert(bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50d", "info": bson.M{ "name": "AVENGERS", "email": "email@something2", "website": "www.gotg.com", "created": "2015-10-20 02:08:04", "updated": "2015-10-20 02:08:04"}, "db_conf": []bson.M{ bson.M{ "store": "main", "server": "localhost", "port": 27017, "database": "argotest_egroups_eudat", "username": "", "password": ""}, }, "users": []bson.M{ bson.M{ "name": "eudat_user", "email": "*****@*****.**", "api_key": "KEY2"}, }}) // get dbconfiguration based on the tenant // Prepare the request object request, _ := http.NewRequest("GET", "", strings.NewReader("")) // add the content-type header to application/json request.Header.Set("Content-Type", "application/json") // add the authentication token which is seeded in testdb request.Header.Set("x-api-key", "KEY1") // authenticate user's api key and find corresponding tenant suite.tenantDbConf, err = authentication.AuthenticateTenant(request.Header, suite.cfg) // Now seed the report DEFINITIONS c = session.DB(suite.tenantDbConf.Db).C("reports") c.Insert(bson.M{ "id": "eba61a9e-22e9-4521-9e47-ecaa4a494364", "info": bson.M{ "name": "Report_A", "description": "report aaaaa", "created": "2015-9-10 13:43:00", "updated": "2015-10-11 13:43:00", }, "topology_schema": bson.M{ "group": bson.M{ "type": "NGI", "group": bson.M{ "type": "SITES", }, }, }, "profiles": []bson.M{ bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50b", "type": "metric", "name": "profile1"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e523", "type": "operations", "name": "profile2"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50q", "type": "aggregation", "name": "profile3"}, }, "filter_tags": []bson.M{ bson.M{ "name": "name1", "value": "value1"}, bson.M{ "name": "name2", "value": "value2"}, }}) // seed the status detailed metric data c = session.DB(suite.tenantDbConf.Db).C("status_endpoint_groups") c.Insert(bson.M{ "report": "eba61a9e-22e9-4521-9e47-ecaa4a494364", "date_integer": 20150501, "timestamp": "2015-05-01T00:00:00Z", "endpoint_group": "HG-03-AUTH", "status": "OK", }) c.Insert(bson.M{ "report": "eba61a9e-22e9-4521-9e47-ecaa4a494364", "date_integer": 20150501, "timestamp": "2015-05-01T01:00:00Z", "endpoint_group": "HG-03-AUTH", "status": "CRITICAL", }) c.Insert(bson.M{ "report": "eba61a9e-22e9-4521-9e47-ecaa4a494364", "date_integer": 20150501, "timestamp": "2015-05-01T05:00:00Z", "endpoint_group": "HG-03-AUTH", "status": "OK", }) // get dbconfiguration based on the tenant // Prepare the request object request, _ = http.NewRequest("GET", "", strings.NewReader("")) // add the content-type header to application/json request.Header.Set("Content-Type", "application/json") // add the authentication token which is seeded in testdb request.Header.Set("x-api-key", "KEY2") // authenticate user's api key and find corresponding tenant suite.tenantDbConf, err = authentication.AuthenticateTenant(request.Header, suite.cfg) // Now seed the reports DEFINITIONS c = session.DB(suite.tenantDbConf.Db).C("reports") c.Insert(bson.M{ "id": "eba61a9e-22e9-4521-9e47-ecaa4a494365", "info": bson.M{ "name": "Report_B", "description": "report aaaaa", "created": "2015-9-10 13:43:00", "updated": "2015-10-11 13:43:00", }, "topology_schema": bson.M{ "group": bson.M{ "type": "EUDAT_GROUPS", "group": bson.M{ "type": "EUDAT_SITES", }, }, }, "profiles": []bson.M{ bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50b", "type": "metric", "name": "eudat.CRITICAL"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e523", "type": "operations", "name": "profile2"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50q", "type": "aggregation", "name": "profile3"}, }, "filter_tags": []bson.M{ bson.M{ "name": "name1", "value": "value1"}, bson.M{ "name": "name2", "value": "value2"}, }}) // seed the status detailed metric data c = session.DB(suite.tenantDbConf.Db).C("status_endpoint_groups") c.Insert(bson.M{ "report": "eba61a9e-22e9-4521-9e47-ecaa4a494365", "date_integer": 20150501, "timestamp": "2015-05-01T00:00:00Z", "endpoint_group": "EL-01-AUTH", "status": "OK", }) c.Insert(bson.M{ "report": "eba61a9e-22e9-4521-9e47-ecaa4a494365", "date_integer": 20150501, "timestamp": "2015-05-01T01:00:00Z", "endpoint_group": "EL-01-AUTH", "status": "CRITICAL", }) c.Insert(bson.M{ "report": "eba61a9e-22e9-4521-9e47-ecaa4a494365", "date_integer": 20150501, "timestamp": "2015-05-01T05:00:00Z", "endpoint_group": "EL-01-AUTH", "status": "OK", }) }
// Create function is used to implement the create tenant request. // The request is an http POST request with the tenant description // provided as json structure in the request body func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusOK h := http.Header{} output := []byte("") err := error(nil) charset := "utf-8" //STANDARD DECLARATIONS END // Set Content-Type response Header value contentType := r.Header.Get("Accept") h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) // Try ingest request body body, err := ioutil.ReadAll(io.LimitReader(r.Body, cfg.Server.ReqSizeLimit)) if err != nil { panic(err) } if err := r.Body.Close(); err != nil { panic(err) } incoming := Tenant{} // Parse body json if err := json.Unmarshal(body, &incoming); err != nil { output, _ = respond.MarshalContent(respond.BadRequestBadJSON, contentType, "", " ") code = http.StatusBadRequest return code, h, output, err } // Try to open the mongo session session, err := mongo.OpenSession(cfg.MongoDB) defer session.Close() if err != nil { code = http.StatusInternalServerError return code, h, output, err } // Check if name exists sameName := []Tenant{} filter := bson.M{"info.name": incoming.Info.Name} err = mongo.Find(session, cfg.MongoDB.Db, "tenants", filter, "name", &sameName) if len(sameName) > 0 { code = http.StatusConflict output, err = createMsgView("Tenant with same name already exists", code) return code, h, output, err } // Generate new id incoming.ID = mongo.NewUUID() incoming.Info.Created = time.Now().Format("2006-01-02 15:04:05") incoming.Info.Updated = incoming.Info.Created err = mongo.Insert(session, cfg.MongoDB.Db, "tenants", incoming) if err != nil { panic(err) } // Create view of the results output, err = createRefView(incoming, "Tenant was succesfully created", 201, r) //Render the results into JSON code = http.StatusCreated return code, h, output, err }
// Setup the Test Environment // This function runs before any test and setups the environment // A test configuration object is instantiated using a reference // to testdb: argo_test_details. Also here is are instantiated some expected // xml response validation messages (authorization,crud responses). // Also the testdb is seeded with tenants,reports,metric_profiles and status_metrics func (suite *metricResultTestSuite) SetupTest() { const testConfig = ` [server] bindip = "" port = 8080 maxprocs = 4 cache = false lrucache = 700000000 gzip = true [mongodb] host = "127.0.0.1" port = 27017 db = "ARGO_test_metric_result" ` _ = gcfg.ReadStringInto(&suite.cfg, testConfig) suite.tenantDbConf.Db = "ARGO_test_metric_result" suite.tenantDbConf.Password = "******" suite.tenantDbConf.Username = "******" suite.tenantDbConf.Store = "ar" suite.clientkey = "KEY1" // Create router and confhandler for test suite.confHandler = respond.ConfHandler{suite.cfg} suite.router = mux.NewRouter().StrictSlash(true).PathPrefix("/api/v2/metric_result").Subrouter() HandleSubrouter(suite.router, &suite.confHandler) // Connect to mongo testdb session, _ := mongo.OpenSession(suite.cfg.MongoDB) // Add authentication token to mongo testdb seedAuth := bson.M{"api_key": "S3CR3T"} _ = mongo.Insert(session, suite.cfg.MongoDB.Db, "authentication", seedAuth) // seed mongo session, err := mgo.Dial(suite.cfg.MongoDB.Host) if err != nil { panic(err) } defer session.Close() // seed a tenant to use c := session.DB(suite.cfg.MongoDB.Db).C("tenants") c.Insert(bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50c", "info": bson.M{ "name": "EGI", "email": "email@something2", "website": "www.gotg.com", "created": "2015-10-20 02:08:04", "updated": "2015-10-20 02:08:04"}, "db_conf": []bson.M{ bson.M{ "store": "main", "server": "localhost", "port": 27017, "database": "ARGO_test_metric_result_egi", "username": "", "password": ""}, }, "users": []bson.M{ bson.M{ "name": "egi_user", "email": "*****@*****.**", "api_key": "KEY1"}, }}) // get dbconfiguration based on the tenant // Prepare the request object request, _ := http.NewRequest("GET", "", strings.NewReader("")) // add the content-type header to application/json request.Header.Set("Content-Type", "application/json;") // add the authentication token which is seeded in testdb request.Header.Set("x-api-key", "KEY1") // authenticate user's api key and find corresponding tenant suite.tenantDbConf, err = authentication.AuthenticateTenant(request.Header, suite.cfg) // seed the status detailed metric data c = session.DB(suite.tenantDbConf.Db).C("status_metrics") c.Insert(bson.M{ "monitoring_box": "nagios3.hellasgrid.gr", "date_integer": 20150501, "timestamp": "2015-05-01T00:00:00Z", "service": "CREAM-CE", "host": "cream01.afroditi.gr", "metric": "emi.cream.CREAMCE-JobSubmit", "status": "OK", "time_integer": 0, "previous_state": "OK", "previous_timestamp": "2015-04-30T23:59:00Z", "summary": "Cream status is ok", "message": "Cream job submission test return value of ok", }) c.Insert(bson.M{ "monitoring_box": "nagios3.hellasgrid.gr", "date_integer": 20150501, "timestamp": "2015-05-01T01:00:00Z", "service": "CREAM-CE", "host": "cream01.afroditi.gr", "metric": "emi.cream.CREAMCE-JobSubmit", "status": "CRITICAL", "time_integer": 10000, "previous_state": "OK", "previous_timestamp": "2015-05-01T00:00:00Z", "summary": "Cream status is CRITICAL", "message": "Cream job submission test failed", }) c.Insert(bson.M{ "monitoring_box": "nagios3.hellasgrid.gr", "date_integer": 20150501, "timestamp": "2015-05-01T05:00:00Z", "service": "CREAM-CE", "host": "cream01.afroditi.gr", "metric": "emi.cream.CREAMCE-JobSubmit", "status": "OK", "time_integer": 50000, "previous_state": "CRITICAL", "previous_timestamp": "2015-05-01T01:00:00Z", "summary": "Cream status is ok", "message": "Cream job submission test return value of ok", }) }
// SubmitRecomputation insert a new pending recomputation in the tenants database func SubmitRecomputation(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusAccepted h := http.Header{} output := []byte("") err := error(nil) charset := "utf-8" //STANDARD DECLARATIONS END contentType, err := respond.ParseAcceptHeader(r) h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) if err != nil { code = http.StatusNotAcceptable output, _ = respond.MarshalContent(respond.NotAcceptableContentType, contentType, "", " ") return code, h, output, err } tenantDbConfig, err := authentication.AuthenticateTenant(r.Header, cfg) if err != nil { output, _ = respond.MarshalContent(respond.UnauthorizedMessage, contentType, "", " ") code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status return code, h, output, err } session, err := mongo.OpenSession(tenantDbConfig) if err != nil { code = http.StatusInternalServerError return code, h, output, err } var recompSubmission IncomingRecomputation // urlValues := r.URL.Query() body, err := ioutil.ReadAll(io.LimitReader(r.Body, cfg.Server.ReqSizeLimit)) if err != nil { panic(err) } if err := r.Body.Close(); err != nil { panic(err) } if err := json.Unmarshal(body, &recompSubmission); err != nil { code = 422 // unprocessable entity output = []byte("Unprocessable JSON") return code, h, output, err } now := time.Now() recomputation := MongoInterface{ ID: mongo.NewUUID(), RequesterName: tenantDbConfig.User, RequesterEmail: tenantDbConfig.Email, StartTime: recompSubmission.StartTime, EndTime: recompSubmission.EndTime, Reason: recompSubmission.Reason, Report: recompSubmission.Report, Exclude: recompSubmission.Exclude, Timestamp: now.Format("2006-01-02 15:04:05"), Status: "pending", } err = mongo.Insert(session, tenantDbConfig.Db, recomputationsColl, recomputation) if err != nil { panic(err) } output, err = createSubmitView(recomputation, contentType, r) return code, h, output, err }
// This function runs before any test and setups the environment // A test configuration object is instantiated using a reference // to testdb: AR_test_tenants. Also here is are instantiated some expected // xml response validation messages (authorization,crud responses). // Also the testdb is seeded with two tenants // and with an authorization token:"S3CR3T" func (suite *TenantTestSuite) SetupTest() { // Connect to mongo testdb session, _ := mongo.OpenSession(suite.cfg.MongoDB) // Add authentication token to mongo testdb seedAuth := bson.M{"api_key": "S3CR3T"} _ = mongo.Insert(session, suite.cfg.MongoDB.Db, "authentication", seedAuth) // seed mongo session, err := mgo.Dial(suite.cfg.MongoDB.Host) if err != nil { panic(err) } defer session.Close() // seed first tenant c := session.DB(suite.cfg.MongoDB.Db).C("tenants") c.Insert(bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50b", "info": bson.M{ "name": "AVENGERS", "email": "email@something", "website": "www.avengers.com", "created": "2015-10-20 02:08:04", "updated": "2015-10-20 02:08:04"}, "db_conf": []bson.M{ bson.M{ "store": "ar", "server": "a.mongodb.org", "port": 27017, "database": "ar_db", "username": "******", "password": "******"}, bson.M{ "store": "status", "server": "b.mongodb.org", "port": 27017, "database": "status_db", "username": "******", "password": "******"}, }, "users": []bson.M{ bson.M{ "name": "cap", "email": "*****@*****.**", "api_key": "C4PK3Y"}, bson.M{ "name": "thor", "email": "*****@*****.**", "api_key": "TH0RK3Y"}, }}) // seed second tenant c.Insert(bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50c", "info": bson.M{ "name": "GUARDIANS", "email": "email@something2", "website": "www.gotg.com", "created": "2015-10-20 02:08:04", "updated": "2015-10-20 02:08:04"}, "db_conf": []bson.M{ bson.M{ "store": "ar", "server": "a.mongodb.org", "port": 27017, "database": "ar_db", "username": "******", "password": "******"}, bson.M{ "store": "status", "server": "b.mongodb.org", "port": 27017, "database": "status_db", "username": "******", "password": "******"}, }, "users": []bson.M{ bson.M{ "name": "groot", "email": "*****@*****.**", "api_key": "GR00TK3Y"}, bson.M{ "name": "starlord", "email": "*****@*****.**", "api_key": "ST4RL0RDK3Y"}, }}) }
// This function runs before any test and setups the environment // A test configuration object is instantiated using a reference // to testdb: argo_test_reports. Also here is are instantiated some expected // xml response validation messages (authorization,crud responses). // Also the testdb is seeded with two reports func (suite *ReportTestSuite) SetupTest() { // Connect to mongo testdb session, _ := mongo.OpenSession(suite.cfg.MongoDB) // Add authentication token to mongo testdb seedAuth := bson.M{"api_key": "S3CR3T"} _ = mongo.Insert(session, suite.cfg.MongoDB.Db, "authentication", seedAuth) // seed mongo session, err := mgo.Dial(suite.cfg.MongoDB.Host) if err != nil { panic(err) } defer session.Close() // seed a tenant to use c := session.DB(suite.cfg.MongoDB.Db).C("tenants") c.Insert(bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50c", "info": bson.M{ "name": "GUARDIANS", "email": "email@something2", "website": "www.gotg.com", "created": "2015-10-20 02:08:04", "updated": "2015-10-20 02:08:04"}, "db_conf": []bson.M{ bson.M{ "store": "ar", "server": "localhost", "port": 27017, "database": "argo_test_tenant_reports2_db1", "username": "******", "password": "******"}, bson.M{ "store": "status", "server": "b.mongodb.org", "port": 27017, "database": "reports_db_tenant", "username": "******", "password": "******"}, }, "users": []bson.M{ bson.M{ "name": "cap", "email": "*****@*****.**", "api_key": "C4PK3Y"}, bson.M{ "name": "thor", "email": "*****@*****.**", "api_key": "TH0RK3Y"}, }}) // get dbconfiguration based on the tenant // Prepare the request object request, _ := http.NewRequest("GET", "", strings.NewReader("")) // add the content-type header to application/json request.Header.Set("Accept", "application/json;") // add the authentication token which is seeded in testdb request.Header.Set("x-api-key", "C4PK3Y") // authenticate user's api key and find corresponding tenant suite.tenantDbConf, err = authentication.AuthenticateTenant(request.Header, suite.cfg) c = session.DB(suite.tenantDbConf.Db).C("metric_profiles") c.Insert( bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50b", "name": "profile1", "services": []bson.M{ bson.M{"service": "CREAM-CE", "metrics": []string{ "emi.cream.CREAMCE-JobSubmit", "emi.wn.WN-Bi", "emi.wn.WN-Csh", "emi.wn.WN-SoftVer"}, }, bson.M{"service": "SRMv2", "metrics": []string{"hr.srce.SRM2-CertLifetime", "org.sam.SRM-Del", "org.sam.SRM-Get", "org.sam.SRM-GetSURLs", "org.sam.SRM-GetTURLs", "org.sam.SRM-Ls", "org.sam.SRM-LsDir", "org.sam.SRM-Put"}, }, }, }) c.Insert( bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50c", "name": "ch.cern.SAM.ROC", "services": []bson.M{ bson.M{"service": "CREAM-CE", "metrics": []string{ "emi.cream.CREAMCE-JobSubmit", "emi.wn.WN-Bi", "emi.wn.WN-Csh", "hr.srce.CADist-Check", "hr.srce.CREAMCE-CertLifetime", "emi.wn.WN-SoftVer"}, }, bson.M{"service": "SRMv2", "metrics": []string{"hr.srce.SRM2-CertLifetime", "org.sam.SRM-Del", "org.sam.SRM-Get", "org.sam.SRM-GetSURLs", "org.sam.SRM-GetTURLs", "org.sam.SRM-Ls", "org.sam.SRM-LsDir", "org.sam.SRM-Put"}, }, }, }) c = session.DB(suite.tenantDbConf.Db).C("aggregation_profiles") c.Insert( bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50bq", "name": "profile3", "namespace": "test", "endpoint_group": "sites", "metric_operation": "AND", "profile_operation": "AND", "metric_profile": bson.M{ "name": "roc.critical", "id": "5637d684-1f8e-4a02-a502-720e8f11e432", }, "groups": []bson.M{ bson.M{"name": "compute", "operation": "OR", "services": []bson.M{ bson.M{ "name": "CREAM-CE", "operation": "AND", }, bson.M{ "name": "ARC-CE", "operation": "AND", }, }}, bson.M{"name": "storage", "operation": "OR", "services": []bson.M{ bson.M{ "name": "SRMv2", "operation": "AND", }, bson.M{ "name": "SRM", "operation": "AND", }, }}, }}) c.Insert( bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50c", "name": "cloud", "namespace": "test", "endpoint_group": "sites", "metric_operation": "AND", "profile_operation": "AND", "metric_profile": bson.M{ "name": "roc.critical", "id": "5637d684-1f8e-4a02-a502-720e8f11e432", }, "groups": []bson.M{ bson.M{"name": "compute", "operation": "OR", "services": []bson.M{ bson.M{ "name": "SERVICEA", "operation": "AND", }, bson.M{ "name": "SERVICEB", "operation": "AND", }, }}, bson.M{"name": "images", "operation": "OR", "services": []bson.M{ bson.M{ "name": "SERVICEC", "operation": "AND", }, bson.M{ "name": "SERVICED", "operation": "AND", }, }}, }}) c = session.DB(suite.tenantDbConf.Db).C("operations_profiles") c.Insert( bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e523", "name": "profile2", "available_states": []string{"A,B,C"}, "defaults": bson.M{ "missing": "A", "down": "B", "unknown": "C"}, "operations": []bson.M{ bson.M{ "name": "AND", "truth_table": []bson.M{ bson.M{ "a": "A", "b": "B", "x": "B", }, bson.M{ "a": "A", "b": "C", "x": "C", }, bson.M{ "a": "B", "b": "C", "x": "C", }}}, bson.M{ "name": "OR", "truth_table": []bson.M{ bson.M{ "a": "A", "b": "B", "x": "A", }, bson.M{ "a": "A", "b": "C", "x": "A", }, bson.M{ "a": "B", "b": "C", "x": "B", }}}, }}) // Now seed the report DEFINITIONS c = session.DB(suite.tenantDbConf.Db).C(reportsColl) c.Insert(bson.M{ "id": "eba61a9e-22e9-4521-9e47-ecaa4a494364", "info": bson.M{ "name": "Report_A", "description": "report aaaaa", "created": "2015-9-10 13:43:00", "updated": "2015-10-11 13:43:00", }, "topology_schema": bson.M{ "group": bson.M{ "type": "NGI", "group": bson.M{ "type": "SITE", }, }, }, "profiles": []bson.M{ bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50b", "type": "metric", "name": "profile1"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e523", "type": "operations", "name": "profile2"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50q", "type": "aggregation", "name": "profile3"}, }, "filter_tags": []bson.M{ bson.M{ "name": "name1", "value": "value1"}, bson.M{ "name": "name2", "value": "value2"}, }}) c.Insert(bson.M{ "id": "eba61a9e-22e9-4521-9e47-ecaa4a494360", "info": bson.M{ "name": "Report_B", "description": "report bbb", "created": "2015-10-08 13:43:00", "updated": "2015-10-09 13:43:00", }, "topology_schema": bson.M{ "group": bson.M{ "type": "ARCHIPELAGO", "group": bson.M{ "type": "ISLAND", }, }, }, "profiles": []bson.M{ bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50b", "type": "metric", "name": "profile1"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e523", "type": "operations", "name": "profile2"}, bson.M{ "id": "6ac7d684-1f8e-4a02-a502-720e8f11e50q", "type": "aggregation", "name": "profile3"}, }, "filter_tags": []bson.M{ bson.M{ "name": "name1", "value": "value1"}, bson.M{ "name": "name2", "value": "value2"}, }}) }
//Create a new metric profile func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusOK h := http.Header{} output := []byte("") err := error(nil) charset := "utf-8" //STANDARD DECLARATIONS END contentType, err := respond.ParseAcceptHeader(r) h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) if err != nil { code = http.StatusNotAcceptable output, _ = respond.MarshalContent(respond.NotAcceptableContentType, contentType, "", " ") return code, h, output, err } tenantDbConfig, err := authentication.AuthenticateTenant(r.Header, cfg) if err != nil { output, _ = respond.MarshalContent(respond.UnauthorizedMessage, contentType, "", " ") code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status return code, h, output, err } session, err := mongo.OpenSession(tenantDbConfig) defer mongo.CloseSession(session) if err != nil { code = http.StatusInternalServerError return code, h, output, err } incoming := OpsProfile{} // Try ingest request body body, err := ioutil.ReadAll(io.LimitReader(r.Body, cfg.Server.ReqSizeLimit)) if err != nil { panic(err) } if err := r.Body.Close(); err != nil { panic(err) } // Parse body json if err := json.Unmarshal(body, &incoming); err != nil { output, _ = respond.MarshalContent(respond.BadRequestBadJSON, contentType, "", " ") code = 400 return code, h, output, err } // Validate States var errList []string errList = append(errList, incoming.validateDuplicates()...) errList = append(errList, incoming.validateStates()...) errList = append(errList, incoming.validateMentions()...) if len(errList) > 0 { output, err = createErrView("Validation Error", 422, errList) code = 422 return code, h, output, err } // Generate new id incoming.ID = mongo.NewUUID() err = mongo.Insert(session, tenantDbConfig.Db, "operations_profiles", incoming) if err != nil { panic(err) } // Create view of the results output, err = createRefView(incoming, "Operations Profile successfully created", 201, r) //Render the results into JSON code = 201 return code, h, output, err }
// Create function is used to implement the create report request. // The request is an http POST request with the report description // provided as json structure in the request body func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusCreated h := http.Header{} output := []byte("") err := error(nil) charset := "utf-8" //STANDARD DECLARATIONS END // Set Content-Type response Header value contentType := r.Header.Get("Accept") h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) // Grab Tenant DB configuration from context tenantDbConfig := context.Get(r, "tenant_conf").(config.MongoConfig) //Reading the json input from the request body reqBody, err := ioutil.ReadAll(io.LimitReader(r.Body, cfg.Server.ReqSizeLimit)) if err != nil { return code, h, output, err } input := MongoInterface{} //Unmarshalling the json input into byte form err = json.Unmarshal(reqBody, &input) // Check if json body is malformed if err != nil { output, _ := respond.MarshalContent(respond.MalformedJSONInput, contentType, "", " ") code = http.StatusBadRequest h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } // Try to open the mongo session session, err := mongo.OpenSession(tenantDbConfig) defer session.Close() if err != nil { code = http.StatusInternalServerError return code, h, output, err } // Validate profiles given in report validationErrors := input.ValidateProfiles(session.DB(tenantDbConfig.Db)) if len(validationErrors) > 0 { code = 422 out := respond.UnprocessableEntity out.Errors = validationErrors output = out.MarshalTo(contentType) return code, h, output, err } // Prepare structure for storing query results results := []MongoInterface{} // Check if report with the same name exists in datastore query := searchName(input.Info.Name) err = mongo.Find(session, tenantDbConfig.Db, reportsColl, query, "name", &results) if err != nil { code = http.StatusInternalServerError return code, h, output, err } // If results are returned for the specific name // then we already have an existing report and we must // abort creation notifing the user if len(results) > 0 { // Name was found so print the error message in xml out := respond.ResponseMessage{ Status: respond.StatusResponse{ Message: "Report with the same name already exists", Code: strconv.Itoa(http.StatusConflict), }} output, _ = respond.MarshalContent(out, contentType, "", " ") code = http.StatusConflict h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } input.Info.Created = time.Now().Format("2006-01-02 15:04:05") input.Info.Updated = input.Info.Created input.ID = mongo.NewUUID() // If no report exists with this name create a new one err = mongo.Insert(session, tenantDbConfig.Db, reportsColl, input) if err != nil { code = http.StatusInternalServerError return code, h, output, err } // Notify user that the report has been created. In xml style selfLink := "https://" + r.Host + r.URL.Path + "/" + input.ID output, err = SubmitSuccesful(input, contentType, selfLink) if err != nil { code = http.StatusInternalServerError return code, h, output, err } h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err }
func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusOK h := http.Header{} output := []byte("") err := error(nil) contentType := "text/xml" charset := "utf-8" //STANDARD DECLARATIONS END message := "" //Authentication procedure if authentication.Authenticate(r.Header, cfg) { session, err := mongo.OpenSession(cfg) if err != nil { code = http.StatusInternalServerError return code, h, output, err } name := []string{} namespace := []string{} //Reading the json input reqBody, err := ioutil.ReadAll(r.Body) input := AvailabilityProfileInput{} results := []AvailabilityProfileOutput{} //Unmarshalling the json input into byte form err = json.Unmarshal(reqBody, &input) if err != nil { if err != nil { message = "Malformated json input data" // User provided malformed json input data output, err := messageXML(message) if err != nil { code = http.StatusInternalServerError return code, h, output, err } code = http.StatusBadRequest h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } } //Making sure that no profile with the requested name and namespace combination already exists in the DB name = append(name, input.Name) namespace = append(namespace, input.Namespace) search := AvailabilityProfileSearch{ name, namespace, } query := readOne(search) err = mongo.Find(session, cfg.MongoDB.Db, "aps", query, "name", &results) if err != nil { code = http.StatusInternalServerError return code, h, output, err } if len(results) <= 0 { //If name-namespace combination is unique we insert the new record into mongo query := createOne(input) err = mongo.Insert(session, cfg.MongoDB.Db, "aps", query) if err != nil { code = http.StatusInternalServerError return code, h, output, err } mongo.CloseSession(session) //Providing with the appropriate user response message = "Availability Profile record successfully created" output, err := messageXML(message) //Render the response into XML if err != nil { code = http.StatusInternalServerError return code, h, output, err } h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } else { message = "An availability profile with that name already exists" output, err := messageXML(message) //Render the response into XML if err != nil { code = http.StatusInternalServerError return code, h, output, err } code = http.StatusBadRequest h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } } else { output = []byte(http.StatusText(http.StatusUnauthorized)) code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } }
func Create(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusOK h := http.Header{} output := []byte("") err := error(nil) contentType := "text/xml" charset := "utf-8" //STANDARD DECLARATIONS END message := "" //only authenticated requests triger the handling code if authentication.Authenticate(r.Header, cfg) { session, err := mongo.OpenSession(cfg) if err != nil { code = http.StatusInternalServerError return code, h, output, err } err = r.ParseForm() if err != nil { code = http.StatusInternalServerError return code, h, output, err } urlValues := r.Form now := time.Now() input := RecomputationsInputOutput{ urlValues.Get("start_time"), urlValues.Get("end_time"), urlValues.Get("reason"), urlValues.Get("ngi_name"), urlValues["exclude_site"], "pending", now.Format("2006-01-02 15:04:05"), //urlValues["exclude_sf"], //urlValues["exclude_end_point"], } query := insertQuery(input) err = mongo.Insert(session, "AR", "recalculations", query) if err != nil { code = http.StatusInternalServerError return code, h, output, err } mongo.CloseSession(session) message = "A recalculation request has been filed" output, err := messageXML(message) //Render the response into XML if err != nil { code = http.StatusInternalServerError return code, h, output, err } h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } else { output = []byte(http.StatusText(http.StatusUnauthorized)) code = http.StatusUnauthorized //If wrong api key is passed we return UNAUTHORIZED http status h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) return code, h, output, err } }
// SubmitRecomputation insert a new pending recomputation in the tenants database func SubmitRecomputation(r *http.Request, cfg config.Config) (int, http.Header, []byte, error) { //STANDARD DECLARATIONS START code := http.StatusAccepted h := http.Header{} output := []byte("") err := error(nil) charset := "utf-8" //STANDARD DECLARATIONS END // Set Content-Type response Header value contentType := r.Header.Get("Accept") h.Set("Content-Type", fmt.Sprintf("%s; charset=%s", contentType, charset)) // Grab Tenant DB configuration from context tenantDbConfig := context.Get(r, "tenant_conf").(config.MongoConfig) session, err := mongo.OpenSession(tenantDbConfig) if err != nil { code = http.StatusInternalServerError return code, h, output, err } var recompSubmission IncomingRecomputation // urlValues := r.URL.Query() body, err := ioutil.ReadAll(io.LimitReader(r.Body, cfg.Server.ReqSizeLimit)) if err != nil { panic(err) } if err := r.Body.Close(); err != nil { panic(err) } if err := json.Unmarshal(body, &recompSubmission); err != nil { code = 422 // unprocessable entity output = []byte("Unprocessable JSON") return code, h, output, err } now := time.Now() recomputation := MongoInterface{ ID: mongo.NewUUID(), RequesterName: tenantDbConfig.User, RequesterEmail: tenantDbConfig.Email, StartTime: recompSubmission.StartTime, EndTime: recompSubmission.EndTime, Reason: recompSubmission.Reason, Report: recompSubmission.Report, Exclude: recompSubmission.Exclude, Timestamp: now.Format("2006-01-02 15:04:05"), Status: "pending", } err = mongo.Insert(session, tenantDbConfig.Db, recomputationsColl, recomputation) if err != nil { panic(err) } output, err = createSubmitView(recomputation, contentType, r) return code, h, output, err }
// Setup the Test Environment // This function runs before any test and setups the environment // A test configuration object is instantiated using a reference // to testdb: AR_test. Also here is are instantiated some expected // xml response validation messages (authorization,crud responses). // Also the testdb is seeded with two av.profiles ("ap1","ap2") // and with an authorization token:"S3CR3T" func (suite *AvProfileTestSuite) SetupTest() { const testConfig = ` [server] bindip = "" port = 8080 maxprocs = 4 cache = false lrucache = 700000000 gzip = true [mongodb] host = "127.0.0.1" port = 27017 db = "AR_test" ` _ = gcfg.ReadStringInto(&suite.cfg, testConfig) suite.resp_profileCreated = " <root>\n" + " <Message>Availability Profile record successfully created</Message>\n </root>" suite.resp_profileUpdated = " <root>\n" + " <Message>Availability Profile was successfully updated</Message>\n </root>" suite.resp_profileDeleted = " <root>\n" + " <Message>Availability Profile was successfully deleted</Message>\n </root>" suite.resp_unauthorized = "Unauthorized" suite.resp_no_id = " <root>\n" + " <Message>No profile matching the requested id</Message>\n </root>" suite.resp_bad_json = " <root>\n" + " <Message>Malformated json input data</Message>\n </root>" // Connect to mongo testdb session, _ := mongo.OpenSession(suite.cfg) // Add authentication token to mongo testdb seed_auth := bson.M{"apiKey": "S3CR3T"} _ = mongo.Insert(session, suite.cfg.MongoDB.Db, "authentication", seed_auth) // seed mongo session, err := mgo.Dial(suite.cfg.MongoDB.Host) if err != nil { panic(err) } defer session.Close() // Insert first seed profile c := session.DB(suite.cfg.MongoDB.Db).C("aps") c.Insert(bson.M{"name": "ap1", "namespace": "namespace1", "poems": []string{"poem01"}, "groups": [][]string{ []string{"ap1-service1", "ap1-service2", "ap1-service3"}, []string{"ap1-service4", "ap1-service5", "ap1-service6"}}}) // Insert first seed profile c.Insert(bson.M{"name": "ap2", "namespace": "namespace2", "poems": []string{"poem02"}, "groups": [][]string{ []string{"ap2-service1", "ap2-service2", "ap2-service3"}, []string{"ap2-service4", "ap2-service5", "ap2-service6"}}}) }