|
1 | | -# notification-client |
| 1 | +# Notification API PHP Client [![Build Status] |
| 2 | + |
| 3 | +PHP client for the Notification API ([API docs](https://app.swaggerhub.com/apis/odinuv/notifications-service/1.0.0)). |
| 4 | + |
| 5 | +## Usage |
| 6 | +The client uses two kinds of authorizations - Storage API token for Subscription API (`SubscriptionClient` class) and |
| 7 | +Manage API Application token with scope `notifications:push-event` for the Events API (`EventsClient` class). |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require keboola/notification-api-php-client |
| 11 | +``` |
| 12 | + |
| 13 | +```php |
| 14 | +use Keboola\NotificationClient\EventsClient; |
| 15 | +use Keboola\NotificationClient\Requests\PostEvent\FailedJobEventData; |
| 16 | +use Keboola\NotificationClient\Requests\PostEvent\JobData; |
| 17 | +use Keboola\NotificationClient\Requests\Event; |
| 18 | +use Psr\Log\NullLogger; |
| 19 | + |
| 20 | +$client = new EventsClient( |
| 21 | + new NullLogger(), |
| 22 | + 'http://notifications.connection.keboola.com/', |
| 23 | + 'xxx-xxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' |
| 24 | +); |
| 25 | +$client->postEvent( |
| 26 | + new Event( |
| 27 | + 'job_failed', |
| 28 | + new FailedJobEventData( |
| 29 | + 'job failed', |
| 30 | + new JobData('my-project', '123', 'http://someUrl', '2020-01-02', '2020-01-01', 'my-orchestration') |
| 31 | + ) |
| 32 | + ) |
| 33 | +); |
| 34 | +``` |
| 35 | + |
| 36 | +## Development |
| 37 | +- Create an Azure service principal to download the required images and login: |
| 38 | + |
| 39 | + ```bash |
| 40 | + SERVICE_PRINCIPAL_NAME=[USERNAME]-notification-api-pull |
| 41 | + ACR_REGISTRY_ID=$(az acr show --name keboolapes --query id --output tsv --subscription c5182964-8dca-42c8-a77a-fa2a3c6946ea) |
| 42 | + SP_PASSWORD=$(az ad sp create-for-rbac --name http://$SERVICE_PRINCIPAL_NAME --scopes $ACR_REGISTRY_ID --role acrpull --query password --output tsv) |
| 43 | + SP_APP_ID=$(az ad sp show --id http://$SERVICE_PRINCIPAL_NAME --query appId --output tsv) |
| 44 | + ``` |
| 45 | + |
| 46 | +- Login and check that you can pull the image: |
| 47 | + |
| 48 | + ```bash |
| 49 | + docker login keboolapes.azurecr.io --username $SP_APP_ID --password $SP_PASSWORD |
| 50 | + docker pull keboolapes.azurecr.io/notification-service:latest |
| 51 | + ``` |
| 52 | + |
| 53 | +- Add the credentials to the k8s cluster: |
| 54 | + |
| 55 | + ```bash |
| 56 | + kubectl create secret docker-registry regcred --docker-server="https://keboolapes.azurecr.io" --docker-username="$SP_APP_ID" --docker-password="$SP_PASSWORD" --namespace dev-notification-client |
| 57 | + kubectl patch serviceaccount default -p "{\"imagePullSecrets\":[{\"name\":\"regcred\"}]}" --namespace dev-notification-client |
| 58 | + ``` |
| 59 | + |
| 60 | +- Set the following environment variables in `set-env.sh` file (use `set-env.template.sh` as sample): |
| 61 | + - `STORAGE_API_URL` - Keboola Connection URL. |
| 62 | + - `TEST_STORAGE_API_TOKEN` - Token to a test project. |
| 63 | + - `TEST_MANAGE_API_APPLICATION_TOKEN` - Application token with scope `notifications:push-event`. |
| 64 | + |
| 65 | +- Set one of Azure or AWS resources (or both, but only one is needed). |
| 66 | + |
| 67 | +### AWS Setup |
| 68 | +- Create a user (`NotificationUser`) for local development using the `provisioning/aws.json` CF template. |
| 69 | + - Create AWS key for the created user. |
| 70 | + - Set the following environment variables in `set-env.sh` file (use `set-env.template.sh` as sample): |
| 71 | + - `TEST_AWS_ACCESS_KEY` - The created security credentials for the `JobQueueApiPhpClient` user. |
| 72 | + - `TEST_AWS_SECRET_ACCESS_KEY` - The created security credentials for the `JobQueueApiPhpClient` user. |
| 73 | + - `AWS_REGION` - `Region` output of the above stack. |
| 74 | + - `AWS_LOGS_S3_BUCKET` - `S3LogsBucket` output of the above stackk. |
| 75 | + |
| 76 | +### Azure Setup |
| 77 | + |
| 78 | +- Create a resource group: |
| 79 | + ```bash |
| 80 | + az account set --subscription "Keboola DEV PS Team CI" |
| 81 | + az group create --name testing-notification-api-php-client --location "East US" |
| 82 | + ``` |
| 83 | + |
| 84 | +- Create a service principal: |
| 85 | + ```bash |
| 86 | + az ad sp create-for-rbac --name testing-notification-api-php-client |
| 87 | + ``` |
| 88 | + |
| 89 | +- Use the response to set values `TEST_AZURE_CLIENT_ID`, `TEST_AZURE_CLIENT_SECRET` and `TEST_AZURE_TENANT_ID` in the `set-env.sh` file: |
| 90 | + ```json |
| 91 | + { |
| 92 | + "appId": "268a6f05-xxxxxxxxxxxxxxxxxxxxxxxxxxx", //-> TEST_AZURE_CLIENT_ID |
| 93 | + "displayName": "testing-notification-api-php-client", |
| 94 | + "name": "http://testing-notification-api-php-client", |
| 95 | + "password": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", //-> TEST_AZURE_CLIENT_SECRET |
| 96 | + "tenant": "9b85ee6f-xxxxxxxxxxxxxxxxxxxxxxxxxxx" //-> TEST_AZURE_TENANT_ID |
| 97 | + } |
| 98 | + ``` |
| 99 | + |
| 100 | +- Get ID of the service principal: |
| 101 | + ```bash |
| 102 | + SERVICE_PRINCIPAL_ID=$(az ad sp list --display-name testing-notification-api-php-client --query "[0].objectId" --output tsv) |
| 103 | + ``` |
| 104 | + |
| 105 | +- Deploy the key vault, provide tenant ID, service principal ID and group ID from the previous commands: |
| 106 | + ```bash |
| 107 | + az deployment group create --resource-group testing-job-queue-api-php-client --template-file provisioning/azure.json --parameters vault_name=test-job-queue-client tenant_id=$TEST_AZURE_TENANT_ID service_principal_object_id=$SERVICE_PRINCIPAL_ID |
| 108 | + ``` |
| 109 | + |
| 110 | +- Show Key Vault URL |
| 111 | + ```bash |
| 112 | + az keyvault show --name test-job-queue-client --query "properties.vaultUri" --output tsv |
| 113 | + ``` |
| 114 | + |
| 115 | +returns e.g. `https://test-job-queue-client.vault.azure.net/`, use this to set values in `set-env.sh` file: |
| 116 | + - `test_azure_key_vault_url` - https://test-job-queue-client.vault.azure.net/ |
| 117 | + |
| 118 | +## Generate environment configuration |
| 119 | + |
| 120 | +```bash |
| 121 | +export DATABASE_URL_BASE64=$(printf "mysql://root:root@dev-mysql-service:3310/notifications?serverVersion=8.0" | base64 --wrap=0) |
| 122 | +export DATABASE_PASSWORD_BASE64=$(printf "root" | base64 --wrap=0) |
| 123 | +export TEST_AZURE_CLIENT_SECRET_BASE64=$(printf "%s" "$TEST_AZURE_CLIENT_SECRET"| base64 --wrap=0) |
| 124 | +export TEST_AWS_SECRET_ACCESS_KEY_BASE64=$(printf "%s" "$TEST_AWS_SECRET_ACCESS_KEY"| base64 --wrap=0) |
| 125 | +export AZURE_LOGS_ABS_CONNECTION_STRING_BASE64=$(printf "%s" "$AZURE_LOGS_ABS_CONNECTION_STRING"| base64 --wrap=0) |
| 126 | +
|
| 127 | +./set-env.sh |
| 128 | +envsubst < provisioning/environments.yaml.template > provisioning/environments.yaml |
| 129 | +kubectl apply -f provisioning/environments.yaml |
| 130 | +kubectl apply -f provisioning/notification.yaml |
| 131 | +TEST_NOTIFICATION_API_IP=`kubectl get svc --output jsonpath --template "{.items[?(@.metadata.name==\"dev-notification-service\")].status.loadBalancer.ingress[].ip}" --namespace=dev-notification-client` |
| 132 | +
|
| 133 | +printf "TEST_NOTIFICATION_API_URL: http://%s:8181" "$TEST_NOTIFICATION_API_IP" |
| 134 | +``` |
| 135 | + |
| 136 | +Store the result `TEST_NOTIFICATION_API_URL` in `set-env.sh`. |
| 137 | + |
| 138 | + |
| 139 | +## Run tests |
| 140 | +- With the above setup, you can run tests: |
| 141 | + |
| 142 | + ```bash |
| 143 | + docker-compose build |
| 144 | + source ./set-env.sh && docker-compose run tests |
| 145 | + ``` |
| 146 | + |
| 147 | +- To run tests with local code use: |
| 148 | + |
| 149 | + ```bash |
| 150 | + docker-compose run tests-local composer install |
| 151 | + source ./set-env.sh && docker-compose run tests-local |
| 152 | + ``` |
0 commit comments