subscriptions

package
v0.0.0-...-c9f6891 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

WARNING (alpha API and code)

This is alpha API and code; do not use it from third party software that you are not willing to rewrite or throw away very soon and often...

You have been warned :).

Configure admin users in pantahub

By default we assign the demouser "admin" permissions to grant subscriptions for pantahub.

You can change that through adding a comma separated list of prns for the following env configurations. The following are the defaults we use:

	PANTAHUB_ADMINS="prn:pantahub.com:auth:/admin"
	PANTAHUB_SUBSCRIPTION_ADMINS=""

Remember that you have to set password explictely if you want to enable the admin demo user.

To do define the admin password as env also with:

	PANTAHUB_DEMOACCOUNTS_PASSWORD_admin=YOURPASSWORDHERE

Set Subscription (as Admin user)

First log in as admin user, e.g.

TOK=`http POST https://api.pantahub.com/auth/login username=admin password=YOURPASSWORDHERE | jq  -r .token`

Then you can create or update the subscription plan for any given prn using the following rest call:

http PUT https://api.pantahub.com/subscriptions/admin/subscription \
	subject=prn:::accounts:/XXXXXXXXXXXXXXXXXXX \
	plan=prn::subscriptions:VIP \
	Authorization:" Bearer $TOK"

Right now the following plans are available to choose from:

	prn::subscriptions:FREE
	prn::subscriptions:VIP
	prn::subscriptions:CUSTOM

You can overwrite default properties of plan through a json map that you can pass in as 'attrs' argument:

http PUT https://api.pantahub.com/subscriptions/admin/subscription \
	subject=prn:::accounts:/XXXXXXXXXXXXXXXXXXX \
	plan=prn::subscriptions:CUSTOM \
	attrs:='{"BANDWDITH": "10GiB", "OBJECTS": "60GiB", "DEVICES": "200"}' \
	Authorization:" Bearer $TOK"

The effects should be visible for users right away in the UI and on dash endpoint.

See your subscription details (as user)

First login as normal user, e.g. with "user1" demoaccount:

TOK1=`http POST https://api.pantahub.com/auth/login username=user1 password=YOURUSER1PASSWORT | jq -r .token`

Next you can get your subscription status through simple GET against the subscriptions main endpoint:

http GET https://api2.pantahub.com/subscriptions/ Authorization:" Bearer $TOK1"
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Mon, 05 Mar 2018 20:54:40 GMT
Server: nginx/1.13.5
Strict-Transport-Security: max-age=15724800; includeSubDomains;
Transfer-Encoding: chunked
X-Powered-By: go-json-rest

{
    "Page": -1,
    "Size": 1,
    "Start": 0,
    "Subs": [
        {
            "attr": {
                "BANDWIDTH": "100GiB",
                "DEVICES": "100",
                "OBJECTS": "20GiB"
            },
            "history": [
                {
                    "attr": {
                        "BANDWIDTH": "100GiB",
                        "DEVICES": "100",
                        "OBJECTS": "20GiB"
                    },
                    "id": "5a9dab7a9764eb000731cc60",
                    "issuer": "prn:pantahub.com:auth:/admin",
                    "last-modified": "2018-03-05T20:42:09.2Z",
                    "prn": "prn::subscriptions:/5a9dab7a9764eb000731cc60",
                    "service": "prn::subscriptions:",
                    "subject": "prn:::accounts:/59ef9e241e7e6b000d3d2bc7",
                    "time-created": "2018-03-05T20:41:30.58Z",
                    "type": "prn::subscriptions:VIP"
                }
            ],
            "id": "5a9dab7a9764eb000731cc60",
            "issuer": "prn:pantahub.com:auth:/admin",
            "last-modified": "2018-03-05T20:42:22.047Z",
            "prn": "prn::subscriptions:/5a9dab7a9764eb000731cc60",
            "service": "prn::subscriptions:",
            "subject": "prn:::accounts:/59ef9e241e7e6b000d3d2bc7",
            "time-created": "2018-03-05T20:41:30.58Z",
            "type": "prn::subscriptions:VIP"
        }
    ]
}

See your quotas on the dash endpoint

Simply log in as normal user like in section above and then query dash api:

$ http GET https://api2.pantahub.com/dash/ Authorization:" Bearer $TOK1"
HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json; charset=utf-8
Date: Mon, 05 Mar 2018 20:55:53 GMT
Server: nginx/1.13.5
Strict-Transport-Security: max-age=15724800; includeSubDomains;
Transfer-Encoding: chunked
X-Powered-By: go-json-rest

{
    "nick": "user1",
    "prn": "prn:::accounts:/user1",
    "subscription": {
        "billing": {
            "AmountDue": 0,
            "Currency": "USD",
            "Type": "Monthly",
            "VatRegion": "World"
        },
        "plan-id": "VIP",
        "quota-stats": {
            "BANDWIDTH": {
                "Actual": 0,
                "Max": 100,
                "Name": "BANDWIDTH",
                "Unit": "GiB"
            },
            "DEVICES": {
                "Actual": 1,
                "Max": 100,
                "Name": "DEVICES",
                "Unit": "Piece"
            },
            "OBJECTS": {
                "Actual": 0.06,
                "Max": 20,
                "Name": "OBJECTS",
                "Unit": "GiB"
            }
        }
    },
    "top-devices": [
    ]
}

Have fun!

Documentation

Overview

Copyright 2020 Pantacor Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2020 Pantacor Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Copyright 2020 Pantacor Ltd.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Index

Constants

View Source
const (

	// SubscriptionTypeCustom custom subscription
	SubscriptionTypeCustom = utils.Prn("prn::subscriptions:CUSTOM")

	// SubscriptionTypeCancelled canceled subscription
	SubscriptionTypeCancelled = utils.Prn("prn::subscriptions:CANCELLED")

	// SubscriptionTypeStripe stripe subscription
	SubscriptionTypeStripe = utils.Prn("prn::subscriptions:stripe")

	// SubscriptionTypeLocked locked subscription
	SubscriptionTypeLocked = utils.Prn("prn::subscriptions:LOCKED")

	// SubscriptionTypePrefix prefix for all subscription
	SubscriptionTypePrefix = utils.Prn("prn::subscriptions:")

	// SubscriptionTypeFree free subscription
	SubscriptionTypeFree = utils.Prn(SubscriptionTypePrefix + "FREE")

	// SubscriptionTypeVIP vip subscription
	SubscriptionTypeVIP = utils.Prn(SubscriptionTypePrefix + "VIP")
)

Variables

View Source
var (
	// SubscriptionProperties define the subscriptions capabilities
	SubscriptionProperties = map[utils.Prn]interface{}{
		SubscriptionTypeFree: map[string]interface{}{
			"OBJECTS":   "2GiB",
			"BANDWIDTH": "2GiB",
			"DEVICES":   "25",
		},
		SubscriptionTypeVIP: map[string]interface{}{
			"OBJECTS":   "20GiB",
			"BANDWIDTH": "10GiB",
			"DEVICES":   "100",
		},
		SubscriptionTypeLocked:    nil,
		SubscriptionTypeCancelled: nil,
		SubscriptionTypeCustom: map[string]interface{}{
			"OBJECTS":   "0GiB",
			"BANDWIDTH": "0GiB",
			"DEVICES":   "0",
		},
		SubscriptionTypeStripe: map[string]interface{}{
			"OBJECTS":   "0GiB",
			"BANDWIDTH": "0GiB",
			"DEVICES":   "0",
		},
	}
)

Functions

This section is empty.

Types

type App

type App struct {
	API *rest.Api
	// contains filtered or unexported fields
}

App subscription rest application

func New

func New(jwtMiddleware *jwt.JWTMiddleware, subscriptionService SubscriptionService, mongoClient *mongo.Client) *App

New create a new subscription rest application

func (*App) MakeHandler

func (s *App) MakeHandler() http.Handler

MakeHandler make the api handler

type Subscription

type Subscription interface {
	GetID() string
	GetPrn() utils.Prn
	GetPlan() utils.Prn
	GetIssuer() utils.Prn
	GetSubject() utils.Prn
	GetService() utils.Prn
	GetTimeModified() time.Time
	GetTimeCreated() time.Time

	GetPeriodStart() time.Time
	GetPeriodEnd() time.Time
	GetPeriodProgression() float64

	HasProperty(key string) bool
	GetProperty(key string) interface{}

	IsLocked() bool
	IsCancelled() bool

	GetHistory() []Subscription

	// UpdatePlan changes plan for subscription to plan PRN. If not nil,
	// attrs will overload the defaults that come with plan.
	UpdatePlan(ctx context.Context, issuer utils.Prn, plan utils.Prn, attrs map[string]interface{}) error
	Cancel(ctx context.Context, issuer utils.Prn) error
	Lock(ctx context.Context, issuer utils.Prn) error
}

Subscription define a subscription interface

type SubscriptionMgo

type SubscriptionMgo struct {

	// The ID for the subscription in mongo
	ID string `json:"id" bson:"_id"`

	// The Prn of the subscription
	Prn utils.Prn `json:"prn" bson:"prn"`

	// The Type of the subscription in PRN format
	Type utils.Prn `json:"type" bson:"type"`

	// the subject of a subscription (service consumer!)
	Subject utils.Prn `json:"subject" bson:"subject"`

	// the issuer of a subscription (service operator!)
	Issuer utils.Prn `json:"issuer" bson:"issuer"`

	// the service a subscription is valid for (e.g. prn::services:/pantahub-base)
	Service utils.Prn `json:"service" bson:"service"`

	// the time this subscription was modified.
	LastModified time.Time `json:"last-modified" bson:"last-modified"`

	// the time this subscription was modified.
	TimeCreated time.Time `json:"time-created" bson:"time-created"`

	// History log in cronological order (earliest first) . Max history is not implemented rightnow..
	History []SubscriptionMgo `json:"history,omitempty" bson:"history,omitempty"`

	Attributes map[string]interface{} `json:"attr,omitempty" bson:"attr,omitempty"`
	// contains filtered or unexported fields
}

SubscriptionMgo define Subscription mongo payload

func (SubscriptionMgo) Cancel

func (i SubscriptionMgo) Cancel(ctx context.Context, issuer utils.Prn) error

Cancel cancel a subscription

func (SubscriptionMgo) GetHistory

func (i SubscriptionMgo) GetHistory() []Subscription

GetHistory get the history of a subscription

func (SubscriptionMgo) GetID

func (i SubscriptionMgo) GetID() string

GetID get subscription ID

func (SubscriptionMgo) GetIssuer

func (i SubscriptionMgo) GetIssuer() utils.Prn

GetIssuer get subscription issuer

func (SubscriptionMgo) GetPeriodEnd

func (i SubscriptionMgo) GetPeriodEnd() time.Time

GetPeriodEnd get when the current period end

func (SubscriptionMgo) GetPeriodProgression

func (i SubscriptionMgo) GetPeriodProgression() float64

GetPeriodProgression get the progression of the current period

func (SubscriptionMgo) GetPeriodStart

func (i SubscriptionMgo) GetPeriodStart() time.Time

GetPeriodStart get when the current period of the subscription started

func (SubscriptionMgo) GetPlan

func (i SubscriptionMgo) GetPlan() utils.Prn

GetPlan get subscription type

func (SubscriptionMgo) GetPrn

func (i SubscriptionMgo) GetPrn() utils.Prn

GetPrn get subscription PRN

func (SubscriptionMgo) GetProperty

func (i SubscriptionMgo) GetProperty(key string) interface{}

GetProperty get a subscription property

func (SubscriptionMgo) GetService

func (i SubscriptionMgo) GetService() utils.Prn

GetService get subscription service

func (SubscriptionMgo) GetSubject

func (i SubscriptionMgo) GetSubject() utils.Prn

GetSubject get subscription subject

func (SubscriptionMgo) GetTimeCreated

func (i SubscriptionMgo) GetTimeCreated() time.Time

GetTimeCreated Get the time when the subscription was created

func (SubscriptionMgo) GetTimeModified

func (i SubscriptionMgo) GetTimeModified() time.Time

GetTimeModified last time the subscription was modified

func (SubscriptionMgo) HasProperty

func (i SubscriptionMgo) HasProperty(key string) bool

HasProperty check if a subscription has a specific property

func (SubscriptionMgo) IsCancelled

func (i SubscriptionMgo) IsCancelled() bool

IsCancelled check if the subscription is cancelled

func (SubscriptionMgo) IsLocked

func (i SubscriptionMgo) IsLocked() bool

IsLocked check if the subscription is locked

func (SubscriptionMgo) Lock

func (i SubscriptionMgo) Lock(ctx context.Context, issuer utils.Prn) error

Lock lock a subscription

func (SubscriptionMgo) UpdatePlan

func (i SubscriptionMgo) UpdatePlan(ctx context.Context, issuer utils.Prn, plan utils.Prn, attrs map[string]interface{}) error

UpdatePlan udpdate a plan with new configuration and saved the previous as history

type SubscriptionPage

type SubscriptionPage struct {
	Start int
	Page  int
	Size  int
	Subs  []Subscription
}

SubscriptionPage pagination for subscription

type SubscriptionReq

type SubscriptionReq struct {
	Subject utils.Prn              `json:"subject"`
	Plan    utils.Prn              `json:"plan"`
	Attrs   map[string]interface{} `json:"attrs"`
}

SubscriptionReq subscription request

type SubscriptionService

type SubscriptionService interface {

	// Delete Subscription
	Delete(pctx context.Context, sub Subscription) error

	New(
		pctx context.Context,
		Subject utils.Prn,
		Issuer utils.Prn,
		Type utils.Prn,
		schema map[string]interface{}) (Subscription, error)

	// Check if user prn is in admins set for this instance
	IsAdmin(user utils.Prn) bool

	// Load subscription by ID
	Load(pctx context.Context, ID string) (Subscription, error)

	// Load subscription by ID
	LoadBySubject(pctx context.Context, subject utils.Prn) (Subscription, error)

	// Load subscription by ID
	GetDefaultSubscription(subject utils.Prn) Subscription

	// List subscription by owning "subject"
	List(pctx context.Context, Subject utils.Prn, start, page int) (SubscriptionPage, error)

	// Save subscription
	Save(pctx context.Context, sub Subscription) error

	// Now time
	Now() time.Time
}

SubscriptionService Interface offers primitives for loading, listing, saving and deleting of subscriptions.

func NewService

func NewService(
	mongoClient *mongo.Client,
	servicePrn utils.Prn,
	admins []utils.Prn,
	typeDefs map[utils.Prn]interface{}) SubscriptionService

NewService creates a new mongoClient backed subscription service Will use the default DB configured in mongoClient provided as arg.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL