Skip to content

Commit

Permalink
Merge branch 'feature/service_instance' into 'develop'
Browse files Browse the repository at this point in the history
Feature/service instance

Closes #1, #2, #3, #4, #6, and #5

See merge request Orange-OpenSource/lfn/onap/python-onapsdk!44
  • Loading branch information
morganrOL committed May 18, 2020
2 parents 02ed10c + 9ae4d0b commit c983e0c
Show file tree
Hide file tree
Showing 77 changed files with 7,943 additions and 337 deletions.
211 changes: 106 additions & 105 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,107 +1,108 @@
---
stages:
- linting
- unit_test
- build
- test
- deploy

image: docker:git
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
# Variables for Container-Scanning.gitlab-ci.yml
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE #/$CI_COMMIT_REF_SLUG
CI_APPLICATION_TAG: $CI_COMMIT_REF_SLUG #$CI_COMMIT_SHA
# Variable for pylint/pydocstyle/SAST/Code-Quality.gitlab-ci.yml
SRC_PATH: '/src'
DOC_PATH: '/docs'

.before_script_docker: &before_script_docker
before_script:
- docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY"

build_master:
stage: build
<<: *before_script_docker
script:
- docker build -t "$CI_REGISTRY_IMAGE:latest" .
- docker push "$CI_REGISTRY_IMAGE:latest"
only:
- master
except:
variables:
- $JOBS_DISABLED

build_testing:
stage: build
<<: *before_script_docker
script:
- docker build -t "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}" .
- docker push "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}"
only:
- branches
except:
refs:
- master
variables:
- $JOBS_DISABLED

build_stable:
stage: build
<<: *before_script_docker
script:
- docker build -t "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}" .
- docker push "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}"
only:
- tags
except:
variables:
- $JOBS_DISABLED

integration_tests:
stage: test
stages:
- linting
- unit_test
- build
- test
- deploy

image: docker:git
services:
- name: registry.gitlab.com/orange-opensource/lfn/onap/mock_servers/mock-sdc:develop
alias: sdc.api.fe.simpledemo.onap.org
image: python:3.7
allow_failure: true
script:
- pip install .
- pip install pytest mock # mock is needed as pytest parse all files before selection
- mv setup.cfg setup.old # pytest tries to use setup.cfg but we don't want
- pytest --verbose --junitxml=pytest-integration.xml integration_tests
artifacts:
reports:
junit: pytest-*.xml
except:
variables:
- $JOBS_DISABLED

pages:
stage: deploy
image:
name: python:3.7
script:
- chmod +x scripts/build_all_branches_in.sh
- scripts/build_all_branches_in.sh
artifacts:
paths:
- public
except:
variables:
- $JOBS_DISABLED

include:
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pylint.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pytest.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pydocstyle.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/doc8.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/Container-Scanning.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/Code-Quality.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/SAST.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pyup.gitlab-ci.yml'
- template: License-Scanning.gitlab-ci.yml
- template: Dependency-Scanning.gitlab-ci.yml

- docker:dind
variables:
DOCKER_DRIVER: overlay
# Variables for Container-Scanning.gitlab-ci.yml
CI_APPLICATION_REPOSITORY: $CI_REGISTRY_IMAGE #/$CI_COMMIT_REF_SLUG
CI_APPLICATION_TAG: $CI_COMMIT_REF_SLUG #$CI_COMMIT_SHA
# Variable for pylint/pydocstyle/SAST/Code-Quality.gitlab-ci.yml
SRC_PATH: '/src'
DOC_PATH: '/docs'

.before_script_docker: &before_script_docker
before_script:
- docker login -u gitlab-ci-token -p "$CI_BUILD_TOKEN" "$CI_REGISTRY"

build_master:
stage: build
<<: *before_script_docker
script:
- docker build -t "$CI_REGISTRY_IMAGE:latest" .
- docker push "$CI_REGISTRY_IMAGE:latest"
only:
- master
except:
variables:
- $JOBS_DISABLED

build_testing:
stage: build
<<: *before_script_docker
script:
- docker build -t "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}" .
- docker push "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}"
only:
- branches
except:
refs:
- master
variables:
- $JOBS_DISABLED

build_stable:
stage: build
<<: *before_script_docker
script:
- docker build -t "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}" .
- docker push "$CI_REGISTRY_IMAGE:${CI_COMMIT_REF_SLUG}"
only:
- tags
except:
variables:
- $JOBS_DISABLED

integration_tests:
stage: test
services:
- name: registry.gitlab.com/orange-opensource/lfn/onap/mock_servers/mock-sdc:develop
alias: sdc.api.fe.simpledemo.onap.org
image: python:3.7
allow_failure: true
script:
- pip install .
- pip install pytest mock # mock is needed as pytest parse all files before selection
- mv setup.cfg setup.old # pytest tries to use setup.cfg but we don't want
- pytest --verbose --junitxml=pytest-integration.xml integration_tests
artifacts:
reports:
junit: pytest-*.xml
except:
variables:
- $JOBS_DISABLED

pages:
stage: deploy
image:
name: python:3.7
script:
- chmod +x scripts/build_all_branches_in.sh
- scripts/build_all_branches_in.sh
artifacts:
paths:
- public
except:
variables:
- $JOBS_DISABLED

include:
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pylint.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pytest.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pydocstyle.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/doc8.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/Container-Scanning.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/Code-Quality.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/SAST.gitlab-ci.yml'
- remote: 'https://gitlab.com/Orange-OpenSource/lfn/ci_cd/gitlab-ci-templates/raw/master/pyup.gitlab-ci.yml'
- template: License-Scanning.gitlab-ci.yml
- template: Dependency-Scanning.gitlab-ci.yml


2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install_requires =
requests[socks]==2.23.0
jinja2==2.11.2
simplejson==3.17.0
oyaml==0.9
setup_requires =
pytest-runner==5.2
tests_require =
Expand All @@ -29,7 +30,6 @@ tests_require =
pytest-cov
pytest-mock
requests-mock
oyaml

[options.packages.find]
where=src
Expand Down
7 changes: 7 additions & 0 deletions src/onapsdk/aai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
"""ONAP SDK AAI package."""

# from .aai_element import *
# from .cloud_infrastructure import *
# from .instances import *
# from .service_design_and_creation import *
99 changes: 99 additions & 0 deletions src/onapsdk/aai/aai_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env python3 pylint: disable=C0302
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: Apache-2.0
"""AAI Element module."""
from dataclasses import dataclass, field
from typing import Dict, Iterator, List, Optional

from onapsdk.onap_service import OnapService
from onapsdk.utils.headers_creator import headers_aai_creator
from onapsdk.utils.jinja import jinja_env


@dataclass
class Relationship:
"""Relationship class.
A&AI elements could have relationship with other A&AI elements.
Relationships are represented by this class objects.
"""

related_to: str
related_link: str
relationship_data: List[Dict[str, str]]
relationship_label: str = ""
related_to_property: List[Dict[str, str]] = field(default_factory=list)


class AaiElement(OnapService):
"""Mother Class of all A&AI elements."""

name: str = "AAI"
server: str = "AAI"
base_url = "https://aai.api.sparky.simpledemo.onap.org:30233"
api_version = "/aai/v16"
headers = headers_aai_creator(OnapService.headers)

@classmethod
def filter_none_key_values(cls, dict_to_filter: Dict[str, Optional[str]]) -> Dict[str, str]:
"""Filter out None key values from dictionary.
Iterate throught given dictionary and filter None values.
Args:
dict_to_filter (Dict): Dictionary to filter out None
Returns:
Dict[str, str]: Filtered dictionary
"""
return dict(
filter(lambda key_value_tuple: key_value_tuple[1] is not None, dict_to_filter.items(),)
)

@property
def url(self) -> str:
"""Resource's url.
Returns:
str: Resource's url
"""
raise NotImplementedError

@property
def relationships(self) -> Iterator[Relationship]:
"""Resource relationships iterator.
Yields:
Relationship: resource relationship
"""
for relationship in self.send_message_json("GET",
"Get object relationships",
f"{self.url}/relationship-list")\
.get("relationship", []):
yield Relationship(
related_to=relationship.get("related-to"),
relationship_label=relationship.get("relationship-label"),
related_link=relationship.get("related-link"),
relationship_data=relationship.get("relationship-data"),
)

def add_relationship(self, relationship: Relationship) -> None:
"""Add relationship to aai resource.
Add relationship to resource using A&AI API
Args:
relationship (Relationship): Relationship to add
"""
self.send_message(
"PUT",
"add relationship to cloud region",
f"{self.url}/relationship-list/relationship",
data=jinja_env()
.get_template("aai_add_relationship.json.j2")
.render(relationship=relationship),
)
Loading

0 comments on commit c983e0c

Please sign in to comment.