Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Add field that controls auto upgrades for a farm #150

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions models/generated/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
)

type Farm struct {
ID schema.ID `bson:"_id" json:"id"`
ThreebotId int64 `bson:"threebot_id" json:"threebot_id"`
IyoOrganization string `bson:"iyo_organization" json:"iyo_organization"`
Name string `bson:"name" json:"name"`
WalletAddresses []WalletAddress `bson:"wallet_addresses" json:"wallet_addresses"`
Location Location `bson:"location" json:"location"`
Email schema.Email `bson:"email" json:"email"`
ResourcePrices []NodeResourcePrice `bson:"resource_prices" json:"resource_prices"`
PrefixZero schema.IPRange `bson:"prefix_zero" json:"prefix_zero"`
ID schema.ID `bson:"_id" json:"id"`
ThreebotId int64 `bson:"threebot_id" json:"threebot_id"`
IyoOrganization string `bson:"iyo_organization" json:"iyo_organization"`
Name string `bson:"name" json:"name"`
WalletAddresses []WalletAddress `bson:"wallet_addresses" json:"wallet_addresses"`
Location Location `bson:"location" json:"location"`
Email schema.Email `bson:"email" json:"email"`
ResourcePrices []NodeResourcePrice `bson:"resource_prices" json:"resource_prices"`
PrefixZero schema.IPRange `bson:"prefix_zero" json:"prefix_zero"`
AutomaticUpgrades bool `bson:"automatic_upgrades" json:"automatic_upgrades"`
}

func NewFarm() (Farm, error) {
Expand Down
12 changes: 1 addition & 11 deletions models/generated/phonebook/phonebook.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package phonebook

import (
"encoding/json"

schema "github.com/threefoldtech/tfexplorer/schema"
)

Expand All @@ -13,14 +11,6 @@ type User struct {
Pubkey string `bson:"pubkey" json:"pubkey"`
Host string `bson:"host" json:"host"`
Description string `bson:"description" json:"description"`
Signature string `bson:"-" json:"signature,omitempty"`
}

func NewUser() (User, error) {
const value = "{}"
var object User
if err := json.Unmarshal([]byte(value), &object); err != nil {
return object, err
}
return object, nil
AutomaticUpgradAgreement bool `bson:"automatic_upgrade_agreement" json:"automatic_upgrade_agreement"`
}
8 changes: 8 additions & 0 deletions pkg/directory/types/farm.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,11 @@ func FarmUpdate(ctx context.Context, db *mongo.Database, id schema.ID, farm Farm
_, err := col.UpdateOne(ctx, f, bson.M{"$set": farm})
return err
}

// FarmEnableAutoUpgrade enable auto upgrade on all the farms owned by farmerID
func FarmEnableAutoUpgrade(ctx context.Context, db *mongo.Database, farmerID int64) error {
f := FarmFilter{}.WithOwner(farmerID)
col := db.Collection(FarmCollection)
_, err := col.UpdateMany(ctx, f, bson.M{"$set": bson.M{"automatic_upgrades": true}})
return err
}
5 changes: 5 additions & 0 deletions pkg/phonebook/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ func UserUpdate(ctx context.Context, db *mongo.Database, id schema.ID, signature
current.Host = update.Host
}

// AutomaticUpgradAgreement can only be switch to true and never switched back
if !current.AutomaticUpgradAgreement && update.AutomaticUpgradAgreement {
current.AutomaticUpgradAgreement = true
}

// actually update the user with final data
if _, err := db.Collection(UserCollection).UpdateOne(ctx, filter, bson.M{"$set": current}); err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions pkg/phonebook/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/pkg/errors"
"github.com/threefoldtech/tfexplorer/models"
"github.com/threefoldtech/tfexplorer/mw"
dtypes "github.com/threefoldtech/tfexplorer/pkg/directory/types"
"github.com/threefoldtech/tfexplorer/pkg/phonebook/types"
"github.com/threefoldtech/tfexplorer/schema"
"github.com/threefoldtech/zos/pkg/crypto"
Expand Down Expand Up @@ -110,6 +111,13 @@ func (u *UserAPI) register(r *http.Request) (interface{}, mw.Response) {
return nil, mw.Error(err)
}

// when the AutomaticUpgradAgreement is signed, enable auto upgrade on all the farms owned by the user being updated
if payload.User.AutomaticUpgradAgreement == true {
if err := dtypes.FarmEnableAutoUpgrade(r.Context(), db, int64(id)); err != nil {
return nil, mw.Error(err)
}
}

return nil, nil
}

Expand Down