// UpdateRecord handles a request to update a single record provided the mux parameter id. func UpdateRecord(server *app.App) func(w http.ResponseWriter, req *http.Request) { return func(w http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) id := vars["id"] record := &models.Record{ID: id} if ok, err := server.DB.Exists(record); err != nil || !ok { server.Render.JSON(w, http.StatusNotFound, nil) return } updateReq := &models.UpdateRecordRequest{} if err := binding.Bind(req, updateReq); err.Handle(w) { return } if err := server.DB.Get(record); err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error getting record from the database"}) log.Println(err) return } if updateReq.FQDN != record.FQDN { existing, err := models.FindRecordByFQDN(server.DB, updateReq.FQDN) if err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error saving the record to the database"}) log.Println(err) return } if existing.ID != "" { server.Render.JSON(w, http.StatusBadRequest, map[string]string{"error": "fqdn must be unique across the application"}) return } } if updateReq.Owner.ID != record.ID { if ok, err := server.DB.Exists(&models.User{ID: updateReq.Owner.ID}); err != nil || !ok { server.Render.JSON(w, http.StatusBadRequest, map[string]string{"error": "owner does not exist"}) return } } if err := copier.Copy(record, updateReq); err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error updating the record"}) return } record.UpdatedAt = time.Now().Unix() if err := server.DB.Save(record); err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error updating the record"}) log.Println(err) return } server.Render.JSON(w, http.StatusOK, record) } }
// ListDroplets lists all the droplets a user has in Digital Ocean func (interactor DOInteractor) ListDroplets(token string) ([]domain.Droplet, error) { client := getClient(token) doDrops, _, err := client.Droplets.List(nil) if err != nil { return nil, err } droplets := []domain.Droplet{} for _, drops := range doDrops { drp := domain.Droplet{ Name: drops.Name, Region: drops.Region.String(), InstanceName: drops.Size.String(), } networksV4 := []domain.NetworkV4{} for _, net := range drops.Networks.V4 { n := domain.NetworkV4{} copier.Copy(n, net) networksV4 = append(networksV4, n) } networksV6 := []domain.NetworkV6{} for _, net := range drops.Networks.V6 { n := domain.NetworkV6{} copier.Copy(n, net) networksV6 = append(networksV6, n) } droplets = append(droplets, drp) } return droplets, nil }
// CreateRecord handles a request to create a new record. func CreateRecord(server *app.App) func(w http.ResponseWriter, req *http.Request) { return func(w http.ResponseWriter, req *http.Request) { user := &models.User{} user = context.Get(req, "user").(*models.User) recordReq := &models.RecordRequest{} if err := binding.Bind(req, recordReq); err.Handle(w) { return } if isSameAsListener(server.Config.Proxy.HTTP.Listener, recordReq) { server.Render.JSON(w, http.StatusBadRequest, map[string]string{"error": "Handler Host and Handler Port must not be the same as HTTP Listener"}) return } if isSameAsListener(server.Config.Proxy.SSL.Listener, recordReq) { server.Render.JSON(w, http.StatusBadRequest, map[string]string{"error": "Handler Host and Handler Port must not be the same as SSL Listener"}) return } existing, err := models.FindRecordByFQDN(server.DB, recordReq.FQDN) if err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error saving the record to the database"}) log.Println(err) return } if existing.ID != "" { server.Render.JSON(w, http.StatusBadRequest, map[string]string{"error": "fqdn must be unique across the application"}) return } now := time.Now().Unix() record := &models.Record{ CreatedAt: now, UpdatedAt: now, } record.Owner.Email = user.Email record.Owner.ID = user.ID if err := copier.Copy(record, recordReq); err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error saving the record to the database"}) return } if err := server.DB.Save(record); err != nil { server.Render.JSON(w, http.StatusInternalServerError, map[string]string{"error": "there was an error saving the record to the database"}) return } server.Render.JSON(w, http.StatusCreated, record) } }
func (client *Client) AddLog(log *commonsLog.Log) error { var addLogRequestData commonsLog.SingleLog copier.Copy(&addLogRequestData, log) if len(client.Application) == 0 { return NewClientError(ERR_APPLICATION_MANDATORY) } addLogRequestData.Application = client.Application addLogRequestData.ApplicationToken = client.ApplicationToken addLogRequestDataJson, err := json.Marshal(addLogRequestData) if err != nil { return err } addLogRequest, err := http.NewRequest("PUT", client.Url+"/api/log", bytes.NewReader(addLogRequestDataJson)) if err != nil { return err } return commonsCall(addLogRequest) }
// CreateDroplet creates a doplet in Digital Ocean func (interactor DOInteractor) CreateDroplet(droplet domain.DropletRequest, token string) (*Instance, error) { client := getClient(token) dropletRequest := &godo.DropletCreateRequest{ Name: droplet.Name, Region: droplet.Region, Size: droplet.Size, Backups: droplet.Backups, IPv6: droplet.IPv6, PrivateNetworking: droplet.PrivateNetworking, UserData: droplet.UserData, Image: godo.DropletCreateImage{ Slug: droplet.Image, }, } sshkeys := []godo.DropletCreateSSHKey{} for _, key := range droplet.SSHKeys { k := godo.DropletCreateSSHKey{ Fingerprint: key.Fingerprint, } sshkeys = append(sshkeys, k) } dropletRequest.SSHKeys = sshkeys drop, _, err := client.Droplets.Create(dropletRequest) if err != nil { return nil, err } inst := &Instance{ Provider: "digital_ocean", Droplet: domain.Droplet{ ID: drop.ID, Name: droplet.Name, Region: droplet.Region, OperatingSystem: drop.Image.Slug, PrivateNetworking: false, InstanceName: drop.Size.Slug, }, } networksV4 := []domain.NetworkV4{} for _, net := range drop.Networks.V4 { n := domain.NetworkV4{} copier.Copy(n, net) networksV4 = append(networksV4, n) } networksV6 := []domain.NetworkV6{} for _, net := range drop.Networks.V6 { n := domain.NetworkV6{} copier.Copy(n, net) networksV6 = append(networksV6, n) } networks := domain.Networks{ V4: networksV4, V6: networksV6, } inst.Networks = networks inst.SSHKeys = droplet.SSHKeys return inst, nil }