Skip to content

Commit

Permalink
Implemented AppGroup membership feature (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-intelli committed Jun 21, 2023
1 parent 838d8da commit 8757aca
Show file tree
Hide file tree
Showing 6 changed files with 453 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/Api/ApigeeX/Controller/AppGroupMembersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Serializer\AppGroupMembershipSerializer;
use Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\Controller\AbstractController;
use Apigee\Edge\Controller\OrganizationAwareControllerTrait;
use Apigee\Edge\Structure\AttributesProperty;
use Psr\Http\Message\UriInterface;

/**
* Allows to manage appgroup memberships.
*/
class AppGroupMembersController extends AbstractController implements AppGroupMembersControllerInterface
{
use AppGroupAwareControllerTrait;
use OrganizationAwareControllerTrait;

/**
* @var \Apigee\Edge\Serializer\EntitySerializerInterface
*/
protected $serializer;

/** @var string */
protected $organization;

/**
* AppGroupMembersController constructor.
*
* @param string $appGroup
* @param string $organization
* @param \Apigee\Edge\ClientInterface $client
*/
public function __construct(string $appGroup, string $organization, ClientInterface $client)
{
parent::__construct($client);
$this->appGroup = $appGroup;
$this->organization = $organization;
$this->serializer = new AppGroupMembershipSerializer();
}

/**
* {@inheritdoc}
*/
public function getMembers(): AppGroupMembership
{
$response = $this->client->get($this->getBaseEndpointUri());

return $this->serializer->denormalize($this->responseToArray($response), AppGroupMembership::class);
}

/**
* {@inheritdoc}
*/
public function setMembers(AppGroupMembership $members): AppGroupMembership
{
$members = $this->serializer->normalize($members);
$apigeeReservedMembers = new AttributesProperty();

// Adding the new members into the attribute.
$apigeeReservedMembers->add('__apigee_reserved__developer_details', json_encode($members));
$response = $this->client->put(
$this->getBaseEndpointUri(),
(string) json_encode((object) [
'attributes' => $this->serializer->normalize($apigeeReservedMembers),
])
);

return $this->serializer->denormalize($this->responseToArray($response), AppGroupMembership::class);
}

/**
* {@inheritdoc}
*
* TODO: Remove removeMember method as it is not used for AppGroup membership
* As we are storing the Team members details inside __apigee_reserved__developer_details
* attribute, we dont have separate API to delete the members from the attribute.
* So we update the __apigee_reserved__developer_details attribute json at setReservedMembership().
*/
public function removeMember(string $email): void
{
$encoded = rawurlencode($email);
$this->client->delete($this->getBaseEndpointUri()->withPath("{$this->getBaseEndpointUri()->getPath()}/{$encoded}"));
}

/**
* {@inheritdoc}
*/
public function getOrganisationName(): string
{
return $this->organization;
}

/**
* {@inheritdoc}
*/
protected function getBaseEndpointUri(): UriInterface
{
return $this->client->getUriFactory()->createUri("/organizations/{$this->organization}/appgroups/{$this->appGroup}");
}
}
58 changes: 58 additions & 0 deletions src/Api/ApigeeX/Controller/AppGroupMembersControllerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\ApigeeX\Controller;

use Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership;

/**
* Interface AppGroupMembersControllerInterface.
*/
interface AppGroupMembersControllerInterface extends AppGroupAwareControllerInterface
{
/**
* List all developers associated with a appgroup.
*
* @return \Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership
* Array of developers with their optional roles in the appgroup.
*/
public function getMembers(): AppGroupMembership;

/**
* Set (add/update/remove) members of a appgroup.
*
* WARNING! If you pass en empty membership object you remove all developers
* from the appgroup.
*
* @param \Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership $members
* Membership object with the changes to be applied.
*
* @return \Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership
* Membership object with the applied changes, it does not contain all
* members. Use getMembers() to retrieve them.
*/
public function setMembers(AppGroupMembership $members);

/**
* Removes a developer from a appgroup.
*
* @param string $email
* Email address of a developer.
*/
public function removeMember(string $email): void;
}
64 changes: 64 additions & 0 deletions src/Api/ApigeeX/Denormalizer/AppGroupMembershipDenormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\ApigeeX\Denormalizer;

use Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

/**
* Denormalizes appgroup membership data.
*/
class AppGroupMembershipDenormalizer implements DenormalizerInterface
{
/**
* {@inheritdoc}
*/
public function denormalize($data, $type, $format = null, array $context = [])
{
$denormalized = [];
if (!empty($data['attributes'])) {
$data = $data['attributes'];
// Getting the members email id and roles.
$key = array_search('__apigee_reserved__developer_details', array_column($data, 'name'));
$members = json_decode($data[$key]->value);
if (is_array($members) || is_object($members)) {
foreach ($members as $item) {
if (isset($item->developer)) {
$denormalized[$item->developer] = $item->roles ?? null;
}
}
}
}

return new AppGroupMembership($denormalized);
}

/**
* {@inheritdoc}
*/
public function supportsDenormalization($data, $type, $format = null)
{
// Do not apply this on array objects. ArrayDenormalizer takes care of them.
if ('[]' === substr($type, -2)) {
return false;
}

return AppGroupMembership::class === $type || $type instanceof AppGroupMembership;
}
}
51 changes: 51 additions & 0 deletions src/Api/ApigeeX/Normalizer/AppGroupMembershipNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\ApigeeX\Normalizer;

use Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class AppGroupMembershipNormalizer implements NormalizerInterface
{
/**
* {@inheritdoc}
*
* @psalm-suppress InvalidReturnType Returning an object here is required
* for creating a valid Apigee Edge request.
*/
public function normalize($object, $format = null, array $context = [])
{
$normalized = [];

/** @var \Apigee\Edge\Api\ApigeeX\Structure\AppGroupMembership $object */
foreach ($object->getMembers() as $member => $role) {
$normalized[] = ['developer' => $member, 'roles' => $role];
}

return $normalized;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
return $data instanceof AppGroupMembership;
}
}
39 changes: 39 additions & 0 deletions src/Api/ApigeeX/Serializer/AppGroupMembershipSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apigee\Edge\Api\ApigeeX\Serializer;

use Apigee\Edge\Api\ApigeeX\Denormalizer\AppGroupMembershipDenormalizer;
use Apigee\Edge\Api\ApigeeX\Normalizer\AppGroupMembershipNormalizer;
use Apigee\Edge\Serializer\EntitySerializer;

class AppGroupMembershipSerializer extends EntitySerializer
{
/**
* AppGroupMembershipSerializer constructor.
*
* @param array $normalizers
*/
public function __construct($normalizers = [])
{
$normalizers = array_merge($normalizers, [
new AppgroupMembershipDenormalizer(), new AppgroupMembershipNormalizer(),
]);
parent::__construct($normalizers);
}
}
Loading

0 comments on commit 8757aca

Please sign in to comment.