github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/zones.go (about)

     1  package cisv1
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/IBM-Cloud/bluemix-go/client"
     7  )
     8  
     9  type ResultsCount struct {
    10  	Count int `json:"count"`
    11  }
    12  
    13  type Error struct {
    14  	Code int    `json:"code"`
    15  	Msg  string `json:"message"`
    16  }
    17  
    18  type NameServer struct {
    19  	NameS int64 `json:"0"`
    20  }
    21  
    22  type Zone struct {
    23  	Id                 string   `json:"id"`
    24  	Name               string   `json:"name"`
    25  	Status             string   `json:"status"`
    26  	Paused             bool     `json:"paused"`
    27  	NameServers        []string `json:"name_servers"`
    28  	OriginalNameServer []string `json:"original_name_servers"`
    29  }
    30  
    31  type ZoneResults struct {
    32  	ZoneList    []Zone       `json:"result"`
    33  	ResultsInfo ResultsCount `json:"result_info"`
    34  	Success     bool         `json:"success"`
    35  	Errors      []Error      `json:"errors"`
    36  }
    37  
    38  type ZoneResult struct {
    39  	Zone     Zone     `json:"result"`
    40  	Success  bool     `json:"success"`
    41  	Errors   []Error  `json:"errors"`
    42  	Messages []string `json:"messages"`
    43  }
    44  
    45  type ZoneBody struct {
    46  	Name string `json:"name"`
    47  }
    48  
    49  type ZoneDelete struct {
    50  	Result struct {
    51  		ZoneId string
    52  	} `json:"result"`
    53  	Success  bool     `json:"success"`
    54  	Errors   []Error  `json:"errors"`
    55  	Messages []string `json:"messages"`
    56  }
    57  
    58  type Zones interface {
    59  	ListZones(cisId string) ([]Zone, error)
    60  	GetZone(cisId string, zoneId string) (*Zone, error)
    61  	CreateZone(cisId string, zoneBody ZoneBody) (*Zone, error)
    62  	DeleteZone(cisId string, zoneId string) error
    63  }
    64  
    65  type zones struct {
    66  	client *client.Client
    67  }
    68  
    69  func newZoneAPI(c *client.Client) Zones {
    70  	return &zones{
    71  		client: c,
    72  	}
    73  }
    74  
    75  func (r *zones) ListZones(cisId string) ([]Zone, error) {
    76  	zoneResults := ZoneResults{}
    77  	rawURL := fmt.Sprintf("/v1/%s/zones?page=1", cisId)
    78  	if _, err := r.client.GetPaginated(rawURL, NewDNSPaginatedResources(Zone{}), func(resource interface{}) bool {
    79  		if zone, ok := resource.(Zone); ok {
    80  			zoneResults.ZoneList = append(zoneResults.ZoneList, zone)
    81  			return true
    82  		}
    83  		return false
    84  	}); err != nil {
    85  		return nil, fmt.Errorf("failed to list paginated dns records: %s", err)
    86  	}
    87  	return zoneResults.ZoneList, nil
    88  }
    89  func (r *zones) GetZone(cisId string, zoneId string) (*Zone, error) {
    90  	zoneResult := ZoneResult{}
    91  	rawURL := fmt.Sprintf("/v1/%s/zones/%s", cisId, zoneId)
    92  	_, err := r.client.Get(rawURL, &zoneResult, nil)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return &zoneResult.Zone, nil
    97  }
    98  
    99  func (r *zones) DeleteZone(cisId string, zoneId string) error {
   100  	rawURL := fmt.Sprintf("/v1/%s/zones/%s", cisId, zoneId)
   101  	_, err := r.client.Delete(rawURL)
   102  	if err != nil {
   103  		return err
   104  	}
   105  	return nil
   106  }
   107  
   108  func (r *zones) CreateZone(cisId string, zoneBody ZoneBody) (*Zone, error) {
   109  	zoneResult := ZoneResult{}
   110  	rawURL := fmt.Sprintf("/v1/%s/zones/", cisId)
   111  	_, err := r.client.Post(rawURL, &zoneBody, &zoneResult)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  	return &zoneResult.Zone, nil
   116  }