func (policy *PolicySvc) getPolicy(input interface{}, ctx common.RestContext) (interface{}, error) { idStr := ctx.PathVariables["policyID"] id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return nil, common.NewError404("policy", idStr) } policyDoc, err := policy.store.getPolicy(id, false) log.Printf("Found policy for ID %d: %s (%v)", id, policyDoc, err) return policyDoc, err }
func (tsvc *TenantSvc) listSegments(input interface{}, ctx common.RestContext) (interface{}, error) { log.Println("In listSegments()") idStr := ctx.PathVariables["tenantId"] segments, err := tsvc.store.listSegments(idStr) if err != nil { return nil, err } if len(segments) == 0 { return nil, common.NewError404("segment", "ALL") } return segments, nil }
func (agentStore *agentStore) findNetIf(netif *NetIf) error { db := agentStore.DbStore.Db var count int agentStore.DbStore.Db.Where(netif).First(netif).Count(&count) err := common.GetDbErrors(db) if err != nil { return err } if count == 0 { return common.NewError404("interface", fmt.Sprintf("mac: %s", netif.Mac)) } return nil }
func (tenantStore *tenantStore) getTenant(id string) (Tenant, error) { ten := Tenant{} var count int log.Println("In getTenant()") db := tenantStore.DbStore.Db.Where("id = ?", id).First(&ten).Count(&count) err := common.GetDbErrors(db) if err != nil { return ten, err } if count == 0 { return ten, common.NewError404("tenant", id) } return ten, nil }
func (tenantStore *tenantStore) getSegment(tenantId string, segmentId string) (Segment, error) { seg := Segment{} var count int db := tenantStore.DbStore.Db.Where("tenant_id = ? AND id = ?", tenantId, segmentId). First(&seg).Count(&count) err := common.GetDbErrors(db) if err != nil { return seg, err } if count == 0 { return seg, common.NewError404("segment/tenant", fmt.Sprintf("%s/%s", tenantId, segmentId)) } return seg, nil }
func (policy *PolicySvc) deletePolicyHandler(input interface{}, ctx common.RestContext) (interface{}, error) { idStr := strings.TrimSpace(ctx.PathVariables["policyID"]) if idStr == "" { if input == nil { return nil, common.NewError400("Request must either be to /policies/{policyID} or have a body.") } policyDoc := input.(*common.Policy) err := policyDoc.Validate() if err != nil { return nil, err } log.Printf("IN deletePolicyHandler with %v", policyDoc) id, err := policy.store.lookupPolicy(policyDoc.ExternalID) if err != nil { // TODO // Important! This should really be done in policy agent. // Only done here as temporary measure. externalId := makeId(policyDoc.AppliedTo, policyDoc.Name) log.Printf("Constructing internal policy name = %s", externalId) policyDoc.ExternalID = externalId id, err = policy.store.lookupPolicy(policyDoc.ExternalID) } log.Printf("Found %d / %v (%T) from external ID %s", id, err, err, policyDoc.ExternalID) if err != nil { return nil, err } return policy.deletePolicy(id) } else { if input != nil { common.NewError400("Request must either be to /policies/{policyID} or have a body.") } id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { return nil, common.NewError404("policy", idStr) } return policy.deletePolicy(id) } }
// deleteEndpoint releases the IP(s) owned by the endpoint into assignable // pool. func (ipamStore *ipamStore) deleteEndpoint(ip string) (Endpoint, error) { tx := ipamStore.DbStore.Db.Begin() results := make([]Endpoint, 0) tx.Where(&Endpoint{Ip: ip}).Find(&results) if len(results) == 0 { tx.Rollback() return Endpoint{}, common.NewError404("endpoint", ip) } if len(results) > 1 { // This cannot happen by constraints... tx.Rollback() errMsg := fmt.Sprintf("Expected one result for ip %s, got %v", ip, results) log.Printf(errMsg) return Endpoint{}, common.NewError500(errors.New(errMsg)) } tx = tx.Model(Endpoint{}).Where("ip = ?", ip).Update("in_use", false) err := common.MakeMultiError(tx.GetErrors()) if err != nil { tx.Rollback() return Endpoint{}, err } tx.Commit() return results[0], nil }