dnspodapi

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: Apache-2.0 Imports: 14 Imported by: 1

README

dnspodapi

Go client wrapper and CTL tool for DNSPOD API

INSTALL

Make sure you have go installed in your env.

go get -u github.com/wangzn/dnspodapi/dpctl

Usage

dpctl help
Config

Default config file is at $HOME/.dnspod.yaml

Demo config file could be found at demo config

Test API token and print API version
# test connection and print info
dpctl info
Domain utils
# list all domains
dpctl domain -a list

# get domain info
dpctl domain -d ashtray.wang,ashtray.name -a info

# create domain and add default record
dpctl domain -d abc.xyz,abcde.xyz -a create

# remove domain
dpctl domain -d abc.xyz,abcde.xyz -a remove

# enable domain
dpctl domain -d abc.xyz,abcde.xyz -a enable

# disable domain
dpctl domain -d abc.xyz,abcde.xyz -a diable

Record utils
# list record of domain ashtray.wang and ashtray.name
dpctl record -a list -d ashtray.wang,ashtray.name

# get record info of domain ashtray.wang, with record proxy,proxy11,proxy12
dpctl record -a info -d ashtray.wang -r proxy,proxy11,proxy12

# disalbe these records and enable them again
dpctl record -a disable -d ashtray.wang -r proxy,proxy11,proxy12
dpctl record -a enable -d ashtray.wang -r proxy,proxy11,proxy12

# create a record
dpctl  record -a create -d ashtray.wang -r testcreate -t A -v 1.1.1.1

# createa record with TTL 6000
dpctl  record -a create -d ashtray.wang -r testcreate -t A -v 1.1.1.1 -T 6000

## import records from local file but DO NOT CLEAR conflict records
dpctl record -a import -d ashtray.wang -f testdata/records.lst

# import records from local file and CLEAR conflict ones
dpctl record -a import -d ashtray.wang -f testdata/records.lst --clear

# import record and force create domain if not exist
dpctl record -a import -d ashtray.wang,abcde.xyz -f testdata/records.lst --force-domain

# export records to file, append if file exist
dpctl record -d ashtray.wang -a export -f /dev/stdout --export-file-mode append

# export records to file, with records filter www21,www22, and overwrite local file if exist
dpctl record -d ashtray.wang -a export -f testdata/export.lst --export-file-mode overwrite -r www11,www12

# ensure records defined in a file, and import into a domain
dpctl record -a ensure -d abc.xyz -f testdata/records.lst 

# ensure records defined in a file, and import into a domain, clear all conflict records, clear records not defined in `records.lst`, and clear NS record of @
dpctl record -a ensure -d abc.xyz -f testdata/records.lst --clear --exclude --force-domain

Variable support in record file
> tail -n 2 testdata/records.lst
cdn1 CNAME proxy1.
cdn2 CNAME www.{DOMAIN}.cdn.com

if the domain variable is abc.xyz, then cdn1 will CNAME to proxy1.abc.xyz, and cdn2 will CNAME to www.abc.xyz.cdn.com

Playbook utils

We can predefined some common used cmds into playbook (named as scene ), and reuse them in short command.

dpctl help playbook
Scene definition

scene is a list of actions , e.g.:

  scene1:
    - auth: "default" # could omit to use default
      category: "domain"
      action: "create"
      subject: "abc.xyz,abcde.xyz"
      params:

    - auth: "default" # could omit to use default
      category: "record"
      action: "import"
      subject: ""
      params:
        domain: "abc.xyz,abcde.xyz"
        clear: "on"
        force_domain: "off"
        record_file: "testdata/record.lst"

    - auth: "default" # could omit to use default
      category: "record"
      action: "export"
      subject: "www,proxy"
      params:
        domain: "abc.xyz,abcde.xyz"
        file_mode: "append" # overwrite, default is exit for no damage
        record_file: "testdata/record.lst"

In this scene above, we define three actions:

  • create domain abc.xyz,abcde.xyz
  • import record from local file testdata/record.lst into domain abc.xyz,abcde.xyz
  • export record into local file testdata/record.lst with record www and proxy in domain abc.xyz,abcde.xyz
Preview scene

then we could preview this scene in case of any unexpected cmds

dpctl playbook -a preview -s scene1
Run scene

finally, we could run this scene

dpctl playbook -a run -s scene1
TODO
  • operate domains
  • add more info commands
  • more config entry for config file
  • add playbook cmd and modify config file structure

Documentation

Index

Constants

View Source
const (
	// StatusEnable defines the string of `enable` of a domain
	StatusEnable = "enable"
	// StatusDisable defines the string of `disable` of a domain
	StatusDisable = "disable"
	// StatusUnkown defines the string of `unkown` of a domain
	StatusUnkown = ""
)
View Source
const (
	// DefaultEndpoint defines the default endpoint URL of dnspodapi
	DefaultEndpoint = "https://dnsapi.cn"
	// DefaultFormat defines the default format of response
	DefaultFormat = "json"
)
View Source
const (
	// ErrUnkownError for unkown
	ErrUnkownError = iota + 1000
	// ErrUnkownModule for unregisterd module
	ErrUnkownModule
	// ErrReflectStructIsNil for nil reflect struct value
	ErrReflectStructIsNil
	// ErrReflectFuncIsNil for nil reflect struct func
	ErrReflectFuncIsNil
	// ErrReflectFuncInvalidReturnValue for invalid return value for reflect func
	ErrReflectFuncInvalidReturnValue
	// ErrInvalidTypeAssertion for invalid type assertion
	ErrInvalidTypeAssertion
	// ErrInvalidStatus for invalid status code
	ErrInvalidStatus
)
View Source
const (
	// DomainModuleName defines the const value of domain module
	DomainModuleName = "domain"
)
View Source
const InfoModuleName = "info"

InfoModuleName defines the module name used in API

View Source
const (
	// RecordModuleName defines the const value of record module
	RecordModuleName = "record"
)

Variables

This section is empty.

Functions

func Err

func Err(code int, args ...interface{}) error

Err returns error

func FormatDomainIDInts added in v1.1.0

func FormatDomainIDInts(rs []DomainEntryIDInt, format string) string

FormatDomainIDInts returns output string

func FormatDomains

func FormatDomains(rs []DomainEntry, format string) string

FormatDomains returns output string

func FormatRecords

func FormatRecords(rs []RecordEntry, format string, domain string) string

FormatRecords returns output string

func GetVersion added in v1.1.0

func GetVersion() (string, error)

GetVersion returns API version

func HTTPResp

func HTTPResp(module, action string, vs url.Values, data url.Values) (int, []byte, error)

HTTPResp returns the http response

func ModuleActionString

func ModuleActionString(module, action string) string

ModuleActionString returns the string to join module and action for URL

func Register

func Register(module string, rf ReflectFunc) error

Register registeres a func with module

func RemoveDomain added in v1.1.0

func RemoveDomain(name string) (bool, error)

RemoveDomain removes domain

func RemoveRecord

func RemoveRecord(domain string, domainID string, recordID string) (bool, error)

RemoveRecord removes a record

func SetAPIToken

func SetAPIToken(appid int, token string)

SetAPIToken set appid and token

func SetDomainStatus added in v1.1.0

func SetDomainStatus(name string, status string) error

SetDomainStatus set domain status enable: enable, 1, online, on disable: disable, 0, offline, off

func SetEndpoint

func SetEndpoint(ep string)

SetEndpoint set the URL endpoint

func SetRecordStatus added in v1.2.0

func SetRecordStatus(domain, domainID, recordID, status string) error

SetRecordStatus set record status 'enable|disable' enable: enable, 1, online, on disable: disable, 0, offline, off

Types

type ActionEntry added in v1.2.0

type ActionEntry struct {
	Auth     string            `json:"auth" yaml:"auth"`
	Category string            `json:"category" yaml:"category"`
	Action   string            `json:"action" yaml:"action"`
	Subject  string            `json:"subject" yaml:"subject"`
	Params   map[string]string `json:"params" yaml:"params"`
}

ActionEntry defines the struct of a action

type ActionResult

type ActionResult struct {
	Code int `json:"code"`
	// Data map[string]string `json:"data"`
	Data interface{} `json:"data"`
	Err  error
}

ActionResult defines the result struct of a action

func Action

func Action(module, action string, data Params) ActionResult

Action runs a command

func ErrActionResult

func ErrActionResult(en int, args ...interface{}) ActionResult

ErrActionResult returns ActionResult with error

type Auth added in v1.2.0

type Auth map[string]AuthEntry

Auth defines the `auth` seciton in config file

type AuthEntry added in v1.2.0

type AuthEntry struct {
	APIID    int    `json:"api_id" yaml:"api_id"`
	APIToken string `json:"api_token" yaml:"api_token"`
}

AuthEntry defines the auth entry

type Config

type Config struct {
	Global   Global   `json:"global" yaml:"global"`
	Auth     Auth     `json:"auth" yaml:"auth"`
	Playbook Playbook `json:"playbook" yaml:"playbook"`
}

Config defines the token info for api

func ParseYamlBytes added in v1.2.0

func ParseYamlBytes(bs []byte) (*Config, error)

ParseYamlBytes parse yaml config bytes into Config struct

func ParseYamlFile added in v1.2.0

func ParseYamlFile(f string) (*Config, error)

ParseYamlFile parses yaml config file into Config struct

type DPA

type DPA struct {
	// contains filtered or unexported fields
}

DPA defines the struct for api related info

type Domain

type Domain struct{}

Domain defines the dummy struct

func (Domain) Create added in v1.1.0

func (d Domain) Create(bs []byte) *DomainCreateResult

Create returns domain created result

func (Domain) Info

func (d Domain) Info(bs []byte) *DomainInfoResult

Info returns domain info

func (Domain) List

func (d Domain) List(bs []byte) *DomainListResult

List returns domain list

func (Domain) Remove added in v1.1.0

func (d Domain) Remove(bs []byte) *DomainRemoveResult

Remove removes a domain

func (Domain) Status added in v1.1.0

func (d Domain) Status(bs []byte) *DomainStatusResult

Status sets status of a domain

type DomainCreateInfo added in v1.1.0

type DomainCreateInfo struct {
	ID        string `json:"id"`
	Punnycode string `json:"punnycode"`
	Domain    string `json:"domain"`
}

DomainCreateInfo defines the struct of field `domain` in create result

type DomainCreateResult added in v1.1.0

type DomainCreateResult struct {
	Status RespCommon       `json:"status"`
	Domain DomainCreateInfo `json:"domain"`
}

DomainCreateResult defines the API result of `create`

type DomainEntry

type DomainEntry struct {
	ID               string `json:"id"`
	Status           string `json:"status"`
	Grade            string `json:"grade"`
	GroupID          string `json:"group_id"`
	SearchenginePush string `json:"searchengine_push"`
	IsMark           string `json:"is_marK"`
	TTL              string `json:"ttl"`
	CnameSpeedup     string `json:"cname_speedup"`
	Remark           string `json:"remark"`
	CreatedOn        string `json:"created_on"`
	UpdatedOn        string `json:"updated_on"`
	Punycode         string `json:"punycode"`
	ExtStatus        string `json:"ext_status"`
	SrcFlag          string `json:"src_flag"`
	Name             string `json:"name"`
	GradeTitle       string `json:"grade_title"`
	IsVIP            string `json:"is_ip"`
	Owner            string `json:"owner"`
	Records          string `json:"records"`
}

DomainEntry defines the API result struct of domain line

func CreateDomain added in v1.1.0

func CreateDomain(domain string) (*DomainEntry, error)

CreateDomain creates a domain entry optional param: group_id, is_mark

func CreateDomainDetail added in v1.1.0

func CreateDomainDetail(domain, groupID, isMark string) (*DomainEntry, error)

CreateDomainDetail with optional params

func GetDomainInfo

func GetDomainInfo(name string) (*DomainEntry, error)

GetDomainInfo returns domain entry

type DomainEntryIDInt

type DomainEntryIDInt struct {
	ID               int    `json:"id"`
	Status           string `json:"status"`
	Grade            string `json:"grade"`
	GroupID          string `json:"group_id"`
	SearchenginePush string `json:"searchengine_push"`
	IsMark           string `json:"is_marK"`
	TTL              string `json:"ttl"`
	CnameSpeedup     string `json:"cname_speedup"`
	Remark           string `json:"remark"`
	CreatedOn        string `json:"created_on"`
	UpdatedOn        string `json:"updated_on"`
	Punycode         string `json:"punycode"`
	ExtStatus        string `json:"ext_status"`
	SrcFlag          string `json:"src_flag"`
	Name             string `json:"name"`
	GradeTitle       string `json:"grade_title"`
	IsVIP            string `json:"is_ip"`
	Owner            string `json:"owner"`
	Records          string `json:"records"`
}

DomainEntryIDInt defines same for DomainEntry only id is int

func GetDomainList

func GetDomainList() ([]DomainEntryIDInt, error)

GetDomainList returns domain entry list

type DomainInfoResult

type DomainInfoResult struct {
	Status RespCommon  `json:"status"`
	Domain DomainEntry `json:"domain"`
}

DomainInfoResult defines the API result of `info“

type DomainListInfo

type DomainListInfo struct {
	DomainTotal   int    `json:"domain_total"`
	AllTotal      int    `json:"all_total"`
	MineTotal     int    `json:"mine_total"`
	ShareTotal    string `json:"share_total"`
	VIPTotal      int    `json:"vip_total"`
	IsmarkTotal   int    `json:"ismark_total"`
	PauseTotal    int    `json:"pause_total"`
	ErrorTotal    int    `json:"error_total"`
	LockTotal     int    `json:"lock_total"`
	SpamTotal     int    `json:"spam_total"`
	VIPExpire     int    `json:"vip_expire"`
	ShareOutTotal int    `json:"share_out_total"`
}

DomainListInfo defines the API struct of `info` field

type DomainListResult

type DomainListResult struct {
	Status  RespCommon         `json:"status"`
	Info    DomainListInfo     `json:"info"`
	Domains []DomainEntryIDInt `json:"domains"`
}

DomainListResult defines the API result of `list`

type DomainRemoveResult added in v1.1.0

type DomainRemoveResult struct {
	Status RespCommon `json:"status"`
}

DomainRemoveResult defines the API result of `remove`

type DomainStatusResult added in v1.1.0

type DomainStatusResult struct {
	Status RespCommon `json:"status"`
}

DomainStatusResult defines the API result of `status`

type Global added in v1.2.0

type Global struct {
	LogOutput string `json:"log_output" yaml:"log_output"`
	Format    string `json:"format" yaml:"format"`
	Auth      string `json:"auth" yaml:"auth"`
}

Global defines the `global` section in config file

type Info

type Info struct{}

Info defines the struct

func (Info) Version

func (i Info) Version(bs []byte) interface{}

Version returns version info

type InfoVersionResult added in v1.1.0

type InfoVersionResult struct {
	Status RespCommon `json:"status"`
}

InfoVersionResult defines the API result of info.version

type Params

type Params struct {
	url.Values
}

Params defines the common data type used in dnspodapi

func P added in v1.1.0

func P() Params

P init a Params

type Playbook added in v1.2.0

type Playbook map[string]Scene

Playbook defines the `playbook` section in config file

type Record

type Record struct{}

Record defines the dummy struct

func (Record) Create

func (r Record) Create(bs []byte) *RecordCreateOrModifyResult

Create returns created record

func (Record) Info

func (r Record) Info(bs []byte) *RecordInfoResult

Info returns record info

func (Record) List

func (r Record) List(bs []byte) *RecordListResult

List returns record list

func (Record) Modify

func (r Record) Modify(bs []byte) *RecordCreateOrModifyResult

Modify returns modified record

func (Record) Remove

func (r Record) Remove(bs []byte) *RecordRemoveResult

Remove returns removed record

func (Record) Status added in v1.2.0

func (r Record) Status(bs []byte) *RecordStatusResult

Status set status of a record

type RecordCreateOrModifyResult

type RecordCreateOrModifyResult struct {
	Status RespCommon  `json:"status"`
	Record RecordEntry `json:"record"`
}

RecordCreateOrModifyResult defines the API result of `create` or `Modify`

type RecordDomainEntry

type RecordDomainEntry struct {
	ID       string   `json:"id"`
	Name     string   `json:"name"`
	TTL      int      `json:"ttl"`
	MinTTL   int      `json:"min_ttl"`
	Status   string   `json:"status"`
	DnspodNS []string `json:"dnspod_ns"`
}

RecordDomainEntry defines the `domain field in `list`

type RecordDomainEntryIDInt

type RecordDomainEntryIDInt struct {
	ID       int      `json:"id"`
	Name     string   `json:"name"`
	TTL      int      `json:"ttl"`
	MinTTL   int      `json:"min_ttl"`
	Status   string   `json:"status"`
	DnspodNS []string `json:"dnspod_ns"`
}

RecordDomainEntryIDInt same as RecordDomainEntry but ID is int

type RecordEntry

type RecordEntry struct {
	ID            string `json:"id"`
	TTL           string `json:"ttl"`
	Value         string `json:"value"`
	Enabled       string `json:"enabled"`
	Status        string `json:"status"`
	UpdatedOn     string `json:"updated_on"`
	Name          string `json:"name"`
	Line          string `json:"line"`
	LineID        string `json:"line_id"`
	Type          string `json:"type"`
	Weight        string `json:"weight"`
	MonitorStatus string `json:"monitor_status"`
	Remark        string `json:"remark"`
	UseAqb        string `json:"use_aqb"`
	MX            string `json:"mx"`
	Hold          string `json:"hold"`
}

RecordEntry defines the API result struct of record line

func CreateRecord

func CreateRecord(domain string, domainID string, data Params) (
	*RecordEntry, error)

CreateRecord creates a new record

func GetRecordInfo

func GetRecordInfo(domain string, domainID string, recordID string) (*RecordEntry, error)

GetRecordInfo returns record entry

func ListRecord

func ListRecord(domain string, domainID string) ([]RecordEntry, error)

ListRecord lists all record by domain

func ModifyRecord

func ModifyRecord(domain string, domainID string, recordID string, data Params) (
	*RecordEntry, error)

ModifyRecord modifies a record

func (RecordEntry) ExportLine added in v1.2.0

func (r RecordEntry) ExportLine() string

ExportLine returns a string line for export

type RecordEntryIDInt added in v1.2.0

type RecordEntryIDInt struct {
	ID            int    `json:"id"`
	TTL           string `json:"ttl"`
	Value         string `json:"value"`
	Enabled       string `json:"enabled"`
	Status        string `json:"status"`
	UpdatedOn     string `json:"updated_on"`
	Name          string `json:"name"`
	Line          string `json:"line"`
	LineID        string `json:"line_id"`
	Type          string `json:"type"`
	Weight        string `json:"weight"`
	MonitorStatus string `json:"monitor_status"`
	Remark        string `json:"remark"`
	UseAqb        string `json:"use_aqb"`
	MX            string `json:"mx"`
	Hold          string `json:"hold"`
}

RecordEntryIDInt is same as RecordEntry but ID field is int

type RecordInfoResult

type RecordInfoResult struct {
	Status RespCommon             `json:"status"`
	Domain RecordDomainEntryIDInt `json:"domain"`
	Record RecordEntry            `json:"record"`
}

RecordInfoResult defines the API result of `info`

type RecordListInfo

type RecordListInfo struct {
	SubDomains  string `json:"sub_domains"`
	RecordTotal string `json:"record_total"`
	RecordsNum  string `json:"records_num"`
}

RecordListInfo defines the `info` field in `list`

type RecordListResult

type RecordListResult struct {
	Status RespCommon `json:"status"`

	Info    RecordListInfo `json:"info"`
	Records []RecordEntry  `json:"records"`
}

RecordListResult defiens the API result of `list`

type RecordRemoveResult

type RecordRemoveResult struct {
	Status RespCommon `json:"status"`
}

RecordRemoveResult defines the API result of `remove`

type RecordStatusResult added in v1.2.0

type RecordStatusResult struct {
	Status RespCommon       `json:"status"`
	Record RecordEntryIDInt `json:"record"`
}

RecordStatusResult defines the API result of `status`

type ReflectFunc

type ReflectFunc func(string, Params) ActionResult

ReflectFunc declares the func format to reflect action to different modules

type RespCommon

type RespCommon struct {
	Code      string `json:"code"`
	Message   string `json:"message"`
	CreatedAt string `json:"created_at"`
}

RespCommon defines common struct in http endpoint

type Scene added in v1.2.0

type Scene []ActionEntry

Scene defines a series entry

Directories

Path Synopsis
dpctl module

Jump to

Keyboard shortcuts

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