image_sharegroups_producer.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // ProducerImageShareGroup represents an ImageShareGroup owned by the producer.
  12  type ProducerImageShareGroup struct {
  13  	ID           int        `json:"id"`
  14  	UUID         string     `json:"uuid"`
  15  	Label        string     `json:"label"`
  16  	Description  string     `json:"description"`
  17  	IsSuspended  bool       `json:"is_suspended"`
  18  	ImagesCount  int        `json:"images_count"`
  19  	MembersCount int        `json:"members_count"`
  20  	Created      *time.Time `json:"-"`
  21  	Updated      *time.Time `json:"-"`
  22  	Expiry       *time.Time `json:"-"`
  23  }
  24  
  25  // UnmarshalJSON implements the json.Unmarshaler interface
  26  func (isg *ProducerImageShareGroup) UnmarshalJSON(b []byte) error {
  27  	type Mask ProducerImageShareGroup
  28  
  29  	p := struct {
  30  		*Mask
  31  
  32  		Created *parseabletime.ParseableTime `json:"created"`
  33  		Updated *parseabletime.ParseableTime `json:"updated"`
  34  		Expiry  *parseabletime.ParseableTime `json:"expiry"`
  35  	}{
  36  		Mask: (*Mask)(isg),
  37  	}
  38  
  39  	if err := json.Unmarshal(b, &p); err != nil {
  40  		return err
  41  	}
  42  
  43  	isg.Created = (*time.Time)(p.Created)
  44  	isg.Updated = (*time.Time)(p.Updated)
  45  	isg.Expiry = (*time.Time)(p.Expiry)
  46  
  47  	return nil
  48  }
  49  
  50  // ImageShareGroupCreateOptions fields are those accepted by CreateImageShareGroup.
  51  type ImageShareGroupCreateOptions struct {
  52  	Label       string                 `json:"label"`
  53  	Description *string                `json:"description,omitempty"`
  54  	Images      []ImageShareGroupImage `json:"images,omitempty"`
  55  }
  56  
  57  // ImageShareGroupUpdateOptions fields are those accepted by UpdateImageShareGroup.
  58  type ImageShareGroupUpdateOptions struct {
  59  	Label       *string `json:"label,omitempty"`
  60  	Description *string `json:"description,omitempty"`
  61  }
  62  
  63  // ImageShareGroupAddImagesOptions fields are those accepted by ImageShareGroupAddImages.
  64  type ImageShareGroupAddImagesOptions struct {
  65  	Images []ImageShareGroupImage `json:"images"`
  66  }
  67  
  68  // ImageShareGroupUpdateImageOptions fields are those accepted by ImageShareGroupUpdateImage.
  69  type ImageShareGroupUpdateImageOptions struct {
  70  	Label       *string `json:"label,omitempty"`
  71  	Description *string `json:"description,omitempty"`
  72  }
  73  
  74  // ImageShareGroupImage represents an Image to be included in a ProducerImageShareGroup.
  75  type ImageShareGroupImage struct {
  76  	ID          string  `json:"id"`
  77  	Label       *string `json:"label,omitempty"`
  78  	Description *string `json:"description,omitempty"`
  79  }
  80  
  81  // ImageShareGroupMember represents a Member of an ImageShareGroup owned by the producer.
  82  type ImageShareGroupMember struct {
  83  	TokenUUID string     `json:"token_uuid"`
  84  	Status    string     `json:"status"`
  85  	Label     string     `json:"label"`
  86  	Created   *time.Time `json:"-"`
  87  	Updated   *time.Time `json:"-"`
  88  	Expiry    *time.Time `json:"-"`
  89  }
  90  
  91  // ImageShareGroupUpdateMemberOptions fields are those accepted by ImageShareGroupUpdateMember.
  92  type ImageShareGroupUpdateMemberOptions struct {
  93  	Label string `json:"label"`
  94  }
  95  
  96  // UnmarshalJSON implements the json.Unmarshaler interface
  97  func (m *ImageShareGroupMember) UnmarshalJSON(b []byte) error {
  98  	type Mask ImageShareGroupMember
  99  
 100  	p := struct {
 101  		*Mask
 102  
 103  		Created *parseabletime.ParseableTime `json:"created"`
 104  		Updated *parseabletime.ParseableTime `json:"updated"`
 105  		Expiry  *parseabletime.ParseableTime `json:"expiry"`
 106  	}{
 107  		Mask: (*Mask)(m),
 108  	}
 109  
 110  	if err := json.Unmarshal(b, &p); err != nil {
 111  		return err
 112  	}
 113  
 114  	m.Created = (*time.Time)(p.Created)
 115  	m.Updated = (*time.Time)(p.Updated)
 116  	m.Expiry = (*time.Time)(p.Expiry)
 117  
 118  	return nil
 119  }
 120  
 121  // ImageShareGroupAddMemberOptions fields are those accepted by ImageShareGroupAddMember.
 122  // The token must be provided to the producer by the consumer via an outside medium.
 123  type ImageShareGroupAddMemberOptions struct {
 124  	Token string `json:"token"`
 125  	Label string `json:"label"`
 126  }
 127  
 128  // ListImageShareGroups lists all ImageShareGroups owned by the producer.
 129  // NOTE: May not currently be available to all users and can only be used with v4beta.
 130  func (c *Client) ListImageShareGroups(
 131  	ctx context.Context,
 132  	opts *ListOptions,
 133  ) ([]ProducerImageShareGroup, error) {
 134  	return getPaginatedResults[ProducerImageShareGroup](
 135  		ctx,
 136  		c,
 137  		"images/sharegroups",
 138  		opts,
 139  	)
 140  }
 141  
 142  // ListImageShareGroupsContainingPrivateImage lists all current ImageShareGroups owned by the producer where
 143  // the given private image is present.
 144  // NOTE: May not currently be available to all users and can only be used with v4beta.
 145  func (c *Client) ListImageShareGroupsContainingPrivateImage(
 146  	ctx context.Context,
 147  	privateImageID string,
 148  	opts *ListOptions,
 149  ) ([]ProducerImageShareGroup, error) {
 150  	return getPaginatedResults[ProducerImageShareGroup](
 151  		ctx,
 152  		c,
 153  		formatAPIPath("images/%s/sharegroups", privateImageID),
 154  		opts,
 155  	)
 156  }
 157  
 158  // GetImageShareGroup gets the specified ImageShareGroup owned by the producer.
 159  // NOTE: May not currently be available to all users and can only be used with v4beta.
 160  func (c *Client) GetImageShareGroup(
 161  	ctx context.Context,
 162  	imageShareGroupID int,
 163  ) (*ProducerImageShareGroup, error) {
 164  	return doGETRequest[ProducerImageShareGroup](
 165  		ctx,
 166  		c,
 167  		formatAPIPath("images/sharegroups/%d", imageShareGroupID),
 168  	)
 169  }
 170  
 171  // CreateImageShareGroup allows the producer to create a new ImageShareGroup.
 172  // NOTE: May not currently be available to all users and can only be used with v4beta.
 173  func (c *Client) CreateImageShareGroup(
 174  	ctx context.Context,
 175  	opts ImageShareGroupCreateOptions,
 176  ) (*ProducerImageShareGroup, error) {
 177  	return doPOSTRequest[ProducerImageShareGroup](
 178  		ctx,
 179  		c,
 180  		"images/sharegroups",
 181  		opts,
 182  	)
 183  }
 184  
 185  // UpdateImageShareGroup allows the producer to update an existing ImageShareGroup's description and label.
 186  // NOTE: May not currently be available to all users and can only be used with v4beta.
 187  func (c *Client) UpdateImageShareGroup(
 188  	ctx context.Context,
 189  	imageShareGroupID int,
 190  	opts ImageShareGroupUpdateOptions,
 191  ) (*ProducerImageShareGroup, error) {
 192  	return doPUTRequest[ProducerImageShareGroup](
 193  		ctx,
 194  		c,
 195  		formatAPIPath("images/sharegroups/%d", imageShareGroupID),
 196  		opts,
 197  	)
 198  }
 199  
 200  // DeleteImageShareGroup deletes the specified ImageShareGroup owned by the producer.
 201  // NOTE: May not currently be available to all users and can only be used with v4beta.
 202  func (c *Client) DeleteImageShareGroup(ctx context.Context, imageShareGroupID int) error {
 203  	return doDELETERequest(
 204  		ctx,
 205  		c,
 206  		formatAPIPath("images/sharegroups/%d", imageShareGroupID),
 207  	)
 208  }
 209  
 210  // ImageShareGroupListImageShareEntries lists the shared image entries of a specified ImageShareGroup owned by the producer.
 211  // NOTE: May not currently be available to all users and can only be used with v4beta.
 212  func (c *Client) ImageShareGroupListImageShareEntries(
 213  	ctx context.Context,
 214  	imageShareGroupID int,
 215  	opts *ListOptions,
 216  ) ([]ImageShareEntry, error) {
 217  	return getPaginatedResults[ImageShareEntry](
 218  		ctx,
 219  		c,
 220  		formatAPIPath("images/sharegroups/%d/images", imageShareGroupID),
 221  		opts,
 222  	)
 223  }
 224  
 225  // ImageShareGroupAddImages allows the producer to add images to a specific ImageShareGroup.
 226  // NOTE: May not currently be available to all users and can only be used with v4beta.
 227  func (c *Client) ImageShareGroupAddImages(
 228  	ctx context.Context,
 229  	imageShareGroupID int,
 230  	opts ImageShareGroupAddImagesOptions,
 231  ) ([]ImageShareEntry, error) {
 232  	return postPaginatedResults[ImageShareEntry, ImageShareGroupAddImagesOptions](
 233  		ctx,
 234  		c,
 235  		formatAPIPath("images/sharegroups/%d/images", imageShareGroupID),
 236  		nil,
 237  		opts,
 238  	)
 239  }
 240  
 241  // ImageShareGroupUpdateImageShareEntry allows the producer to update the description and label of a specified ImageShareEntry within the specified ImageShareGroup.
 242  // NOTE: May not currently be available to all users and can only be used with v4beta.
 243  func (c *Client) ImageShareGroupUpdateImageShareEntry(
 244  	ctx context.Context,
 245  	imageShareGroupID int,
 246  	imageID string,
 247  	opts ImageShareGroupUpdateImageOptions,
 248  ) (*ImageShareEntry, error) {
 249  	return doPUTRequest[ImageShareEntry](
 250  		ctx,
 251  		c,
 252  		formatAPIPath("images/sharegroups/%d/images/%s", imageShareGroupID, imageID),
 253  		opts,
 254  	)
 255  }
 256  
 257  // ImageShareGroupRemoveImage allows the producer to remove access to an image within an ImageShareGroup owned by the producer.
 258  // NOTE: May not currently be available to all users and can only be used with v4beta.
 259  func (c *Client) ImageShareGroupRemoveImage(
 260  	ctx context.Context,
 261  	imageShareGroupID int,
 262  	imageID string,
 263  ) error {
 264  	return doDELETERequest(
 265  		ctx,
 266  		c,
 267  		formatAPIPath("images/sharegroups/%d/images/%s", imageShareGroupID, imageID),
 268  	)
 269  }
 270  
 271  // ImageShareGroupListMembers lists the ImageShareGroupMembers of the provided ImageShareGroup owned by the producer.
 272  // NOTE: May not currently be available to all users and can only be used with v4beta.
 273  func (c *Client) ImageShareGroupListMembers(
 274  	ctx context.Context,
 275  	imageShareGroupID int,
 276  	opts *ListOptions,
 277  ) ([]ImageShareGroupMember, error) {
 278  	return getPaginatedResults[ImageShareGroupMember](
 279  		ctx,
 280  		c,
 281  		formatAPIPath("images/sharegroups/%d/members", imageShareGroupID),
 282  		opts,
 283  	)
 284  }
 285  
 286  // ImageShareGroupGetMember gets the details of the specified ImageShareGroupMember in the specified
 287  // ImageShareGroup owned by the producer.
 288  // NOTE: May not currently be available to all users and can only be used with v4beta.
 289  func (c *Client) ImageShareGroupGetMember(
 290  	ctx context.Context,
 291  	imageShareGroupID int,
 292  	tokenUUID string,
 293  ) (*ImageShareGroupMember, error) {
 294  	return doGETRequest[ImageShareGroupMember](
 295  		ctx,
 296  		c,
 297  		formatAPIPath("images/sharegroups/%d/members/%s", imageShareGroupID, tokenUUID),
 298  	)
 299  }
 300  
 301  // ImageShareGroupAddMember allows the producer to add members to a specific ImageShareGroup.
 302  // NOTE: May not currently be available to all users and can only be used with v4beta.
 303  func (c *Client) ImageShareGroupAddMember(
 304  	ctx context.Context,
 305  	imageShareGroupID int,
 306  	opts ImageShareGroupAddMemberOptions,
 307  ) (*ImageShareGroupMember, error) {
 308  	return doPOSTRequest[ImageShareGroupMember](
 309  		ctx,
 310  		c,
 311  		formatAPIPath("images/sharegroups/%d/members", imageShareGroupID),
 312  		opts,
 313  	)
 314  }
 315  
 316  // ImageShareGroupUpdateMember allows the producer to update the label associated with the specified
 317  // ImageShareGroupMember in the specified ImageShareGroup owned by the producer.
 318  // NOTE: May not currently be available to all users and can only be used with v4beta.
 319  func (c *Client) ImageShareGroupUpdateMember(
 320  	ctx context.Context,
 321  	imageShareGroupID int,
 322  	tokenUUID string,
 323  	opts ImageShareGroupUpdateMemberOptions,
 324  ) (*ImageShareGroupMember, error) {
 325  	return doPUTRequest[ImageShareGroupMember](
 326  		ctx,
 327  		c,
 328  		formatAPIPath("images/sharegroups/%d/members/%s", imageShareGroupID, tokenUUID),
 329  		opts,
 330  	)
 331  }
 332  
 333  // ImageShareGroupRemoveMember allows the producer to remove an individual ImageShareGroupMember
 334  // that’s been accepted into the ImageShareGroup owned by the producer.
 335  // NOTE: May not currently be available to all users and can only be used with v4beta.
 336  func (c *Client) ImageShareGroupRemoveMember(
 337  	ctx context.Context,
 338  	imageShareGroupID int,
 339  	tokenUUID string,
 340  ) error {
 341  	return doDELETERequest(
 342  		ctx,
 343  		c,
 344  		formatAPIPath("images/sharegroups/%d/members/%s", imageShareGroupID, tokenUUID),
 345  	)
 346  }
 347