Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CoE] Content libraries #342

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/ref/modules/all.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Execution Modules
saltext.vmware.modules.cluster
saltext.vmware.modules.cluster_drs
saltext.vmware.modules.cluster_ha
saltext.vmware.modules.content_library
saltext.vmware.modules.datacenter
saltext.vmware.modules.datastore
saltext.vmware.modules.dvportgroup
Expand All @@ -32,6 +33,7 @@ Execution Modules
saltext.vmware.modules.nsxt_uplink_profiles
saltext.vmware.modules.ssl_adapter
saltext.vmware.modules.storage_policies
saltext.vmware.modules.subscribed_library
saltext.vmware.modules.tag
saltext.vmware.modules.vm
saltext.vmware.modules.vmc_dhcp_profiles
Expand Down
6 changes: 6 additions & 0 deletions docs/ref/modules/saltext.vmware.modules.content_library.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

saltext.vmware.modules.content_library
======================================

.. automodule:: saltext.vmware.modules.content_library
:members:
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

saltext.vmware.modules.subscribed_library
=========================================

.. automodule:: saltext.vmware.modules.subscribed_library
:members:
1 change: 1 addition & 0 deletions docs/ref/states/all.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ State Modules
.. autosummary::
:toctree:

saltext.vmware.states.content_library
saltext.vmware.states.datacenter
saltext.vmware.states.datastore
saltext.vmware.states.esxi
Expand Down
6 changes: 6 additions & 0 deletions docs/ref/states/saltext.vmware.states.content_library.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

saltext.vmware.states.content_library
=====================================

.. automodule:: saltext.vmware.states.content_library
:members:
151 changes: 151 additions & 0 deletions src/saltext/vmware/modules/content_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License: Apache-2.0
import logging

import saltext.vmware.utils.connect as connect

log = logging.getLogger(__name__)

__virtualname__ = "vsphere_content_library"


def __virtual__():
return __virtualname__


def list():
"""
Lists IDs for all the content libraries on a given vCenter.
"""
response = connect.request(
"/api/content/local-library", "GET", opts=__opts__, pillar=__pillar__
)
return response["response"].json()


def list_detailed():
"""
Lists all the content libraries on a given vCenter with all their details.
"""
result = {}
library_ids = list()
for library_id in library_ids:
response = get(library_id)
name = response["name"]
result[name] = response
return result


def get(id):
"""
Returns info on given content library.

id
(string) Content Library ID.
"""
url = f"/api/content/local-library/{id}"
response = connect.request(url, "GET", opts=__opts__, pillar=__pillar__)
return response["response"].json()


def create(library):
"""
Creates a new content library.

library
Dictionary having values for library, as following:

name
Name of the content library.

description
(optional) Description for the content library being created.

datastore
Datastore ID where library will store its contents.

published
(optional) Whether the content library should be published or not.

authentication
(optional) The authentication method when the content library is published.
"""

publish_info = {
"published": library["published"],
"authentication_method": library["authentication"],
}
storage_backings = [{"datastore_id": library["datastore"], "type": "DATASTORE"}]

data = {
"name": library["name"],
"publish_info": publish_info,
"storage_backings": storage_backings,
"type": "LOCAL",
}
if "description" in library:
data["description"] = library["description"]

response = connect.request(
"/api/content/local-library", "POST", body=data, opts=__opts__, pillar=__pillar__
)
return response["response"].json()


def update(id, library):
"""
Updates content library with given id.

id
(string) Content library ID .

library
Dictionary having values for library, as following:

name
(optional) Name of the content library.

description
(optional) Description for the content library being updated.

published
(optional) Whether the content library should be published or not.

authentication
(optional) The authentication method when the content library is published.

datastore
(optional) Datastore ID where library will store its contents.
"""

publish_info = {}
if "published" in library:
publish_info["published"] = library["published"]
if "authentication" in library:
publish_info["authentication_method"] = library["authentication"]

data = {}
if "name" in library:
data["name"] = library["name"]
if "description" in library:
publish_info["description"] = library["description"]
if publish_info:
data["publish_info"] = publish_info
if "datastore" in library:
data["storage_backings"] = [{"datastore_id": library["datastore"], "type": "DATASTORE"}]

url = f"/api/content/local-library/{id}"
response = connect.request(url, "PATCH", body=data, opts=__opts__, pillar=__pillar__)
return response["response"].json()


def delete(id):
"""
Delete content library having corresponding id.

id
(string) Content library ID to delete.
"""
url = f"/api/content/local-library/{id}"
response = connect.request(url, "DELETE", opts=__opts__, pillar=__pillar__)
return response["response"].json()
127 changes: 127 additions & 0 deletions src/saltext/vmware/modules/subscribed_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License: Apache-2.0
import logging

import saltext.vmware.utils.connect as connect

log = logging.getLogger(__name__)

__virtualname__ = "vsphere_subscribed_library"


def __virtual__():
return __virtualname__


def list():
"""
Lists IDs for all the subscribed libraries on a given vCenter.
"""
response = connect.request(
"/api/content/subscribed-library", "GET", opts=__opts__, pillar=__pillar__
)
response = response["response"].json()
return response["value"]


def get(id):
"""
Returns info on given subscribed library.

id
(string) Subscribed Library ID.
"""
url = f"/api/content/subscribed-library/{id}"
response = connect.request(url, "GET", opts=__opts__, pillar=__pillar__)
return response["response"].json()


def create(name, description, published, authentication, datastore):
"""
Creates a new subscribed library.

name
Name of the subscribed library.

datastore
Datastore ID where library will store its contents.

description
(optional) Description for the subscribed library being created.

published
(optional) Whether the subscribed library should be published or not.

authentication
(optional) The authentication method for the subscribed library being published.
"""

publish_info = {"published": published, "authentication_method": authentication}
storage_backings = {"datastore_id": datastore, "type": "DATASTORE"}

data = {
"name": name,
"publish_info": publish_info,
"storage_backings": storage_backings,
"type": "SUBSCRIBED",
}
if description is not None:
data["description"] = description

response = connect.request(
"/api/content/subscribed-library", "POST", body=data, opts=__opts__, pillar=__pillar__
)
return response["response"].json()


def update(id, name, description, published, authentication, datastore):
"""
Updates subscribed library with given id.

id
(string) Subscribed library ID .

name
(optional) Name of the subscribed library.

description
(optional) Description for the subscribed library being updated.

published
(optional) Whether the subscribed library should be published or not.

authentication
(optional) The authentication method for the subscribed library being published.

datastore
(optional) Datastore ID where library will store its contents.
"""

publish_info = {"published": published, "authentication_method": authentication}
storage_backings = {"datastore_id": datastore, "type": "DATASTORE"}

data = {}
if name is not None:
data["name"] = name
if description is not None:
data["name"] = description
if published is not None:
data["publish_info"] = publish_info
if datastore is not None:
data["storage_backings"] = storage_backings

url = f"/api/content/subscribed-library/{id}"
response = connect.request(url, "PATCH", body=data, opts=__opts__, pillar=__pillar__)
return response["response"].json()


def delete(id):
"""
Delete subscribed library having corresponding id.

id
(string) Subscribed library ID to delete.
"""
url = f"/api/content/subscribed-library/{id}"
response = connect.request(url, "DELETE", opts=__opts__, pillar=__pillar__)
return response["response"].json()
Loading