common.go raw

   1  package zones
   2  
   3  import (
   4  	"fmt"
   5  	"net/http"
   6  
   7  	"github.com/mimuret/golang-iij-dpf/pkg/api"
   8  	"github.com/mimuret/golang-iij-dpf/pkg/apis"
   9  	"github.com/mimuret/golang-iij-dpf/pkg/schema"
  10  )
  11  
  12  const groupName = "zones.api.dns-platform.jp/v1"
  13  
  14  func register(items ...apis.Spec) {
  15  	schema.NewRegister(groupName).Add(items...)
  16  }
  17  
  18  type Spec interface {
  19  	apis.Spec
  20  	SetZoneID(string)
  21  	GetZoneID() string
  22  }
  23  
  24  type ChildSpec interface {
  25  	Spec
  26  	GetID() int64
  27  	SetID(int64)
  28  }
  29  
  30  type ListSpec interface {
  31  	api.ListSpec
  32  	Spec
  33  }
  34  
  35  type CountableListSpec interface {
  36  	api.CountableListSpec
  37  	Spec
  38  }
  39  
  40  type AttributeMeta struct {
  41  	ZoneID string `read:"-"`
  42  }
  43  
  44  // for ctl
  45  func (s *AttributeMeta) GetGroup() string    { return groupName }
  46  func (s *AttributeMeta) SetZoneID(id string) { s.ZoneID = id }
  47  func (s *AttributeMeta) GetZoneID() string   { return s.ZoneID }
  48  
  49  func GetPathMethodForListSpec(action api.Action, s ListSpec) (string, string) {
  50  	switch action {
  51  	case api.ActionList:
  52  		return http.MethodGet, fmt.Sprintf("/zones/%s/%s", s.GetZoneID(), s.GetName())
  53  	case api.ActionCount:
  54  		if _, ok := s.(api.CountableListSpec); ok {
  55  			return http.MethodGet, fmt.Sprintf("/zones/%s/%s/count", s.GetZoneID(), s.GetName())
  56  		}
  57  	}
  58  	return "", ""
  59  }
  60  
  61  func GetReadPathMethodForSpec(action api.Action, s Spec) (string, string) {
  62  	if action == api.ActionRead {
  63  		return http.MethodGet, fmt.Sprintf("/zones/%s/%s", s.GetZoneID(), s.GetName())
  64  	}
  65  	return "", ""
  66  }
  67